Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in xr.date_range with exactly three parameters provided. #8770

Closed
5 tasks done
rjavierch opened this issue Feb 19, 2024 · 2 comments · Fixed by #8774
Closed
5 tasks done

Error in xr.date_range with exactly three parameters provided. #8770

rjavierch opened this issue Feb 19, 2024 · 2 comments · Fixed by #8774

Comments

@rjavierch
Copy link
Contributor

rjavierch commented Feb 19, 2024

What happened?

I was trying to create a list of a fixed number of N dates from start to end using the "periods" argument of xr.date_range.

What did you expect to happen?

To obtain a DatetimeIndex object with N period's dates between the starting date and the ending date.

Minimal Complete Verifiable Example

import xarray as xr
from datetime import datetime

# Attempt to create a date range with exactly three parameters
xr.date_range(start=datetime(1961, 1, 1), end=datetime(1991, 1, 1), periods=3)

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/roberto/miniconda3/envs/gs1/lib/python3.11/site-packages/xarray/coding/cftime_offsets.py", line 1225, in date_range
    return pd.date_range(
           ^^^^^^^^^^^^^^
  File "/home/roberto/miniconda3/envs/gs1/lib/python3.11/site-packages/pandas/core/indexes/datetimes.py", line 1009, in date_range
    dtarr = DatetimeArray._generate_range(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/roberto/miniconda3/envs/gs1/lib/python3.11/site-packages/pandas/core/arrays/datetimes.py", line 400, in _generate_range
    raise ValueError(
ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specified

Anything else we need to know?

When running:

import pandas as pd
from datetime import datetime

pd.date_range(start=datetime(1961, 1, 1), end=datetime(1991, 1, 1), periods=2)

Outputs:
DatetimeIndex(['1961-01-01', '1991-01-01'], dtype='datetime64[ns]', freq=None)

Environment

/home/roberto/miniconda3/envs/gs1/lib/python3.11/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit: None
python: 3.11.6 | packaged by conda-forge | (main, Oct 3 2023, 10:40:35) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 6.5.0-14-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1

xarray: 2023.12.0
pandas: 2.1.4
numpy: 1.26.4
scipy: 1.12.0
netCDF4: 1.6.3
pydap: None
h5netcdf: 1.3.0
h5py: 3.8.0
Nio: 1.5.5
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: 1.3.5
dask: 2023.12.1
distributed: 2023.12.1
matplotlib: 3.8.2
cartopy: 0.22.0
seaborn: 0.13.0
numbagg: None
fsspec: 2023.12.2
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.2.2
pip: 23.3.2
conda: None
pytest: None
mypy: None
IPython: 8.20.0
sphinx: None

@rjavierch rjavierch added bug needs triage Issue that has not been reviewed by xarray team member labels Feb 19, 2024
Copy link

welcome bot commented Feb 19, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@spencerkclark
Copy link
Member

spencerkclark commented Feb 19, 2024

Thanks for the report. I think this is because we explicitly set freq to "D" by default rather than use logic like this to set it to "D" only if any of periods, start, or end are None. I think we'd happily take a PR to fix this.

In the meantime you should be able to work around this by explicitly setting freq=None in your call.

@spencerkclark spencerkclark added topic-cftime and removed needs triage Issue that has not been reviewed by xarray team member labels Feb 19, 2024
spencerkclark added a commit that referenced this issue Feb 24, 2024
…e None (#8774)

* Fixing issue #8770: Improved frequency parameter logic to set it to 'D' only if periods, start, or end are None.

* Addressed feedback: Updated default argument handling in cftime_range to ensure consistency across date range functions

* Update doc/whats-new.rst

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>

* Update xarray/tests/test_cftime_offsets.py

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>

* Update xarray/tests/test_cftime_offsets.py

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>

* Input argument period included in test_cftime_range_no_freq and test_date_range_no_freq following #8770

* Update doc/whats-new.rst

Co-authored-by: Spencer Clark <spencerkclark@gmail.com>

---------

Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
Co-authored-by: Spencer Clark <spencerkclark@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants