Skip to content

Commit f63ec87

Browse files
rjavierchmathausespencerkclark
authored
date_range: set default freq to 'D' only if periods, start, or end are 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>
1 parent d9760f3 commit f63ec87

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ Deprecations
3737

3838
Bug fixes
3939
~~~~~~~~~
40-
40+
- The default ``freq`` parameter in :py:meth:`xr.date_range` and :py:meth:`xr.cftime_range` is
41+
set to ``'D'`` only if ``periods``, ``start``, or ``end`` are ``None`` (:issue:`8770`, :pull:`8774`).
42+
By `Roberto Chang <https://github.com/rjavierch>`_.
4143

4244
Documentation
4345
~~~~~~~~~~~~~

xarray/coding/cftime_offsets.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ def cftime_range(
914914
start=None,
915915
end=None,
916916
periods=None,
917-
freq="D",
917+
freq=None,
918918
normalize=False,
919919
name=None,
920920
closed: NoDefault | SideOptions = no_default,
@@ -1100,6 +1100,10 @@ def cftime_range(
11001100
--------
11011101
pandas.date_range
11021102
"""
1103+
1104+
if freq is None and any(arg is None for arg in [periods, start, end]):
1105+
freq = "D"
1106+
11031107
# Adapted from pandas.core.indexes.datetimes._generate_range.
11041108
if count_not_none(start, end, periods, freq) != 3:
11051109
raise ValueError(
@@ -1152,7 +1156,7 @@ def date_range(
11521156
start=None,
11531157
end=None,
11541158
periods=None,
1155-
freq="D",
1159+
freq=None,
11561160
tz=None,
11571161
normalize=False,
11581162
name=None,

xarray/tests/test_cftime_offsets.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,6 @@ def test_cftime_range_name():
12941294
(None, None, 5, "YE", None),
12951295
("2000", None, None, "YE", None),
12961296
(None, "2000", None, "YE", None),
1297-
("2000", "2001", None, None, None),
12981297
(None, None, None, None, None),
12991298
("2000", "2001", None, "YE", "up"),
13001299
("2000", "2001", 5, "YE", None),
@@ -1733,3 +1732,47 @@ def test_cftime_range_same_as_pandas(start, end, freq):
17331732
expected = date_range(start, end, freq=freq, use_cftime=False)
17341733

17351734
np.testing.assert_array_equal(result, expected)
1735+
1736+
1737+
@pytest.mark.filterwarnings("ignore:Converting a CFTimeIndex with:")
1738+
@pytest.mark.parametrize(
1739+
"start, end, periods",
1740+
[
1741+
("2022-01-01", "2022-01-10", 2),
1742+
("2022-03-01", "2022-03-31", 2),
1743+
("2022-01-01", "2022-01-10", None),
1744+
("2022-03-01", "2022-03-31", None),
1745+
],
1746+
)
1747+
def test_cftime_range_no_freq(start, end, periods):
1748+
"""
1749+
Test whether cftime_range produces the same result as Pandas
1750+
when freq is not provided, but start, end and periods are.
1751+
"""
1752+
# Generate date ranges using cftime_range
1753+
result = cftime_range(start=start, end=end, periods=periods)
1754+
result = result.to_datetimeindex()
1755+
expected = pd.date_range(start=start, end=end, periods=periods)
1756+
1757+
np.testing.assert_array_equal(result, expected)
1758+
1759+
1760+
@pytest.mark.parametrize(
1761+
"start, end, periods",
1762+
[
1763+
("2022-01-01", "2022-01-10", 2),
1764+
("2022-03-01", "2022-03-31", 2),
1765+
("2022-01-01", "2022-01-10", None),
1766+
("2022-03-01", "2022-03-31", None),
1767+
],
1768+
)
1769+
def test_date_range_no_freq(start, end, periods):
1770+
"""
1771+
Test whether date_range produces the same result as Pandas
1772+
when freq is not provided, but start, end and periods are.
1773+
"""
1774+
# Generate date ranges using date_range
1775+
result = date_range(start=start, end=end, periods=periods)
1776+
expected = pd.date_range(start=start, end=end, periods=periods)
1777+
1778+
np.testing.assert_array_equal(result, expected)

0 commit comments

Comments
 (0)