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

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

Merged
merged 7 commits into from
Feb 24, 2024
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Deprecations

Bug fixes
~~~~~~~~~

- The logic of the frequency parameter in :py:meth:`xr.date_range` and :py:meth:`xr.cftime_range` are
set to 'D' only if periods, start, or end are None. (:issue:`8770`, :pull:`8774`).
rjavierch marked this conversation as resolved.
Show resolved Hide resolved
By `Roberto Chang <https://github.com/rjavierch>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 6 additions & 2 deletions xarray/coding/cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def cftime_range(
start=None,
end=None,
periods=None,
freq="D",
freq=None,
normalize=False,
name=None,
closed: NoDefault | SideOptions = no_default,
Expand Down Expand Up @@ -1100,6 +1100,10 @@ def cftime_range(
--------
pandas.date_range
"""

if freq is None and any(arg is None for arg in [periods, start, end]):
freq = "D"

# Adapted from pandas.core.indexes.datetimes._generate_range.
if count_not_none(start, end, periods, freq) != 3:
raise ValueError(
Expand Down Expand Up @@ -1152,7 +1156,7 @@ def date_range(
start=None,
end=None,
periods=None,
freq="D",
freq=None,
tz=None,
normalize=False,
name=None,
Expand Down
44 changes: 44 additions & 0 deletions xarray/tests/test_cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,8 @@
freq: str | None,
inclusive: Literal["up", None],
) -> None:
with pytest.raises(ValueError):

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 all-but-dask

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.11

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 flaky

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.12

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1310 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.11

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>
cftime_range(start, end, periods, freq, inclusive=inclusive) # type: ignore[arg-type]

Check failure on line 1311 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.9 min-all-deps

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1311 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.9

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>

Check failure on line 1311 in xarray/tests/test_cftime_offsets.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.9

test_invalid_cftime_range_inputs[2000-2001-None-None-None] Failed: DID NOT RAISE <class 'ValueError'>


def test_invalid_cftime_arg() -> None:
Expand Down Expand Up @@ -1733,3 +1733,47 @@
expected = date_range(start, end, freq=freq, use_cftime=False)

np.testing.assert_array_equal(result, expected)


@pytest.mark.filterwarnings("ignore:Converting a CFTimeIndex with:")
@pytest.mark.parametrize(
"start, end",
[
("2022-01-01", "2022-01-10"),
("2022-02-01", "2022-02-28"),
("2022-03-01", "2022-03-31"),
],
)
def test_cftime_range_no_freq(start, end):
"""
Test whether cftime_range produces the same result as Pandas
when freq is not provided, but start and end are.
"""
# Generate date ranges using cftime_range
result = cftime_range(start=start, end=end)
result = result.to_datetimeindex()
expected = pd.date_range(start=start, end=end)

# Assert that the results are equal
rjavierch marked this conversation as resolved.
Show resolved Hide resolved
np.testing.assert_array_equal(result, expected)


@pytest.mark.parametrize(
"start, end",
[
("2022-01-01", "2022-01-10"),
("2022-02-01", "2022-02-28"),
("2022-03-01", "2022-03-31"),
],
)
def test_date_range_no_freq(start, end):
"""
Test whether date_range produces the same result as Pandas
when freq is not provided, but start and end are.
"""
# Generate date ranges using date_range
result = date_range(start=start, end=end)
expected = pd.date_range(start=start, end=end)

# Assert that the results are equal
rjavierch marked this conversation as resolved.
Show resolved Hide resolved
np.testing.assert_array_equal(result, expected)
Loading