From 2eb97a3d05c2d743248458211ac0a5729b78f524 Mon Sep 17 00:00:00 2001 From: Roberto Date: Wed, 21 Feb 2024 14:42:19 +0900 Subject: [PATCH 1/7] Fixing issue #8770: Improved frequency parameter logic to set it to 'D' only if periods, start, or end are None. --- xarray/coding/cftime_offsets.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/coding/cftime_offsets.py b/xarray/coding/cftime_offsets.py index 556bab8504b..aec7b5ea82d 100644 --- a/xarray/coding/cftime_offsets.py +++ b/xarray/coding/cftime_offsets.py @@ -1152,7 +1152,7 @@ def date_range( start=None, end=None, periods=None, - freq="D", + freq=None, tz=None, normalize=False, name=None, @@ -1218,6 +1218,9 @@ def date_range( """ from xarray.coding.times import _is_standard_calendar + if freq is None and any(arg is None for arg in [periods, start, end]): + freq = "D" + if tz is not None: use_cftime = False From 39254e2b712e929695af322874ce1d0df29b3a61 Mon Sep 17 00:00:00 2001 From: Roberto Date: Fri, 23 Feb 2024 00:59:38 +0900 Subject: [PATCH 2/7] Addressed feedback: Updated default argument handling in cftime_range to ensure consistency across date range functions --- doc/whats-new.rst | 4 ++- xarray/coding/cftime_offsets.py | 9 +++--- xarray/tests/test_cftime_offsets.py | 44 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ece209e09ae..372961071a4 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/coding/cftime_offsets.py b/xarray/coding/cftime_offsets.py index aec7b5ea82d..2e594455874 100644 --- a/xarray/coding/cftime_offsets.py +++ b/xarray/coding/cftime_offsets.py @@ -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, @@ -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( @@ -1218,9 +1222,6 @@ def date_range( """ from xarray.coding.times import _is_standard_calendar - if freq is None and any(arg is None for arg in [periods, start, end]): - freq = "D" - if tz is not None: use_cftime = False diff --git a/xarray/tests/test_cftime_offsets.py b/xarray/tests/test_cftime_offsets.py index a0bc678b51c..ed76add3202 100644 --- a/xarray/tests/test_cftime_offsets.py +++ b/xarray/tests/test_cftime_offsets.py @@ -1733,3 +1733,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", + [ + ("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 + 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 + np.testing.assert_array_equal(result, expected) From 1c384b3579e6cd012ce3aec23c784a63dcee4d0e Mon Sep 17 00:00:00 2001 From: Roberto Chang Date: Fri, 23 Feb 2024 08:11:31 +0900 Subject: [PATCH 3/7] Update doc/whats-new.rst Co-authored-by: Mathias Hauser --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 372961071a4..081ae02fa4b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -35,7 +35,7 @@ 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`). + set to ``'D'`` only if periods, start, or end are ``None`` (:issue:`8770`, :pull:`8774`). By `Roberto Chang `_. Documentation From a6d0391cfd724f61dfe2770e1d9efd30353d4e98 Mon Sep 17 00:00:00 2001 From: Roberto Chang Date: Fri, 23 Feb 2024 08:11:42 +0900 Subject: [PATCH 4/7] Update xarray/tests/test_cftime_offsets.py Co-authored-by: Mathias Hauser --- xarray/tests/test_cftime_offsets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/tests/test_cftime_offsets.py b/xarray/tests/test_cftime_offsets.py index ed76add3202..8b1a2b0283b 100644 --- a/xarray/tests/test_cftime_offsets.py +++ b/xarray/tests/test_cftime_offsets.py @@ -1754,7 +1754,6 @@ def test_cftime_range_no_freq(start, end): result = result.to_datetimeindex() expected = pd.date_range(start=start, end=end) - # Assert that the results are equal np.testing.assert_array_equal(result, expected) From 439816b8206751fe3c5d2bc0e1fafa591160266b Mon Sep 17 00:00:00 2001 From: Roberto Chang Date: Fri, 23 Feb 2024 08:11:50 +0900 Subject: [PATCH 5/7] Update xarray/tests/test_cftime_offsets.py Co-authored-by: Mathias Hauser --- xarray/tests/test_cftime_offsets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/tests/test_cftime_offsets.py b/xarray/tests/test_cftime_offsets.py index 8b1a2b0283b..69dcb40edce 100644 --- a/xarray/tests/test_cftime_offsets.py +++ b/xarray/tests/test_cftime_offsets.py @@ -1774,5 +1774,4 @@ def test_date_range_no_freq(start, end): result = date_range(start=start, end=end) expected = pd.date_range(start=start, end=end) - # Assert that the results are equal np.testing.assert_array_equal(result, expected) From 8f47a0c8aa8a1b189cebfde4372af48178389ce3 Mon Sep 17 00:00:00 2001 From: Roberto Date: Fri, 23 Feb 2024 08:44:59 +0900 Subject: [PATCH 6/7] Input argument period included in test_cftime_range_no_freq and test_date_range_no_freq following #8770 --- xarray/tests/test_cftime_offsets.py | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/xarray/tests/test_cftime_offsets.py b/xarray/tests/test_cftime_offsets.py index 69dcb40edce..0110afe40ac 100644 --- a/xarray/tests/test_cftime_offsets.py +++ b/xarray/tests/test_cftime_offsets.py @@ -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), @@ -1737,41 +1736,43 @@ def test_cftime_range_same_as_pandas(start, end, freq): @pytest.mark.filterwarnings("ignore:Converting a CFTimeIndex with:") @pytest.mark.parametrize( - "start, end", + "start, end, periods", [ - ("2022-01-01", "2022-01-10"), - ("2022-02-01", "2022-02-28"), - ("2022-03-01", "2022-03-31"), + ("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): +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 and end are. + when freq is not provided, but start, end and periods are. """ # Generate date ranges using cftime_range - result = cftime_range(start=start, end=end) + result = cftime_range(start=start, end=end, periods=periods) result = result.to_datetimeindex() - expected = pd.date_range(start=start, end=end) + expected = pd.date_range(start=start, end=end, periods=periods) np.testing.assert_array_equal(result, expected) @pytest.mark.parametrize( - "start, end", + "start, end, periods", [ - ("2022-01-01", "2022-01-10"), - ("2022-02-01", "2022-02-28"), - ("2022-03-01", "2022-03-31"), + ("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): +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 and end are. + when freq is not provided, but start, end and periods are. """ # Generate date ranges using date_range - result = date_range(start=start, end=end) - expected = pd.date_range(start=start, end=end) + 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) From 9196cd28dd481af1500f9634a8a70dfd2bcb1546 Mon Sep 17 00:00:00 2001 From: Roberto Chang Date: Sat, 24 Feb 2024 13:08:42 +0900 Subject: [PATCH 7/7] Update doc/whats-new.rst Co-authored-by: Spencer Clark --- doc/whats-new.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 081ae02fa4b..57cea664cac 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -34,8 +34,8 @@ 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`). +- The default ``freq`` parameter in :py:meth:`xr.date_range` and :py:meth:`xr.cftime_range` is + set to ``'D'`` only if ``periods``, ``start``, or ``end`` are ``None`` (:issue:`8770`, :pull:`8774`). By `Roberto Chang `_. Documentation