pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.56k stars 1.07k forks source link

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

Closed rjavierch closed 6 months ago

rjavierch commented 7 months ago

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

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
welcome[bot] commented 7 months ago

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 commented 7 months ago

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.