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`).
By `Roberto Chang <https://github.com/rjavierch>`_.
rjavierch marked this conversation as resolved.
Show resolved Hide resolved

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
45 changes: 44 additions & 1 deletion xarray/tests/test_cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,6 @@ def test_cftime_range_name():
(None, None, 5, "YE", None),
("2000", None, None, "YE", None),
(None, "2000", None, "YE", None),
("2000", "2001", None, None, None),
(None, None, None, None, None),
("2000", "2001", None, "YE", "up"),
("2000", "2001", 5, "YE", None),
Expand Down Expand Up @@ -1733,3 +1732,47 @@ def test_cftime_range_same_as_pandas(start, end, freq):
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, periods",
[
("2022-01-01", "2022-01-10", 2),
("2022-03-01", "2022-03-31", 2),
("2022-01-01", "2022-01-10", None),
("2022-03-01", "2022-03-31", None),
],
)
def test_cftime_range_no_freq(start, end, periods):
"""
Test whether cftime_range produces the same result as Pandas
when freq is not provided, but start, end and periods are.
"""
# Generate date ranges using cftime_range
result = cftime_range(start=start, end=end, periods=periods)
result = result.to_datetimeindex()
expected = pd.date_range(start=start, end=end, periods=periods)

np.testing.assert_array_equal(result, expected)


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

np.testing.assert_array_equal(result, expected)
Loading