Skip to content

Commit 0004584

Browse files
author
maddogghoek
committed
replace deprecated cftime_range with date_range(use_cftime=True) for most unit tests (pydata#9886)
1 parent 40222b8 commit 0004584

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

xarray/tests/test_cftime_offsets.py

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,13 +1255,12 @@ def test_cftime_range(
12551255
assert np.max(np.abs(deltas)) < 0.001
12561256

12571257

1258-
def test_cftime_range_name():
1259-
with pytest.warns(DeprecationWarning):
1260-
result = cftime_range(start="2000", periods=4, name="foo")
1261-
assert result.name == "foo"
1258+
def test_date_range_name():
1259+
result = date_range(start="2000", periods=4, name="foo")
1260+
assert result.name == "foo"
12621261

1263-
result = cftime_range(start="2000", periods=4)
1264-
assert result.name is None
1262+
result = date_range(start="2000", periods=4)
1263+
assert result.name is None
12651264

12661265

12671266
@pytest.mark.parametrize(
@@ -1275,15 +1274,14 @@ def test_cftime_range_name():
12751274
("2000", "2001", 5, "YE", None),
12761275
],
12771276
)
1278-
def test_invalid_cftime_range_inputs(
1277+
def test_invalid_date_range_cftime_inputs(
12791278
start: str | None,
12801279
end: str | None,
12811280
periods: int | None,
12821281
freq: str | None,
12831282
inclusive: Literal["up", None],
12841283
) -> None:
1285-
with pytest.raises(ValueError), pytest.warns(DeprecationWarning):
1286-
cftime_range(start, end, periods, freq, inclusive=inclusive) # type: ignore[arg-type]
1284+
date_range(start, end, periods, freq, inclusive=inclusive, use_cftime=True) # type: ignore[arg-type]
12871285

12881286

12891287
_CALENDAR_SPECIFIC_MONTH_END_TESTS = [
@@ -1307,11 +1305,14 @@ def test_calendar_specific_month_end(
13071305
year = 2000 # Use a leap-year to highlight calendar differences
13081306
date_type = get_date_type(calendar)
13091307
expected = [date_type(year, *args) for args in expected_month_day]
1310-
with pytest.warns(DeprecationWarning):
1311-
result = cftime_range(
1312-
start="2000-02", end="2001", freq="2ME", calendar=calendar
1313-
).values
1314-
np.testing.assert_equal(result, expected)
1308+
result = date_range(
1309+
start="2000-02",
1310+
end="2001",
1311+
freq="2ME",
1312+
calendar=calendar,
1313+
use_cftime=True,
1314+
).values
1315+
np.testing.assert_equal(result, expected)
13151316

13161317

13171318
@pytest.mark.parametrize(
@@ -1325,14 +1326,10 @@ def test_calendar_specific_month_end_negative_freq(
13251326
year = 2000 # Use a leap-year to highlight calendar differences
13261327
date_type = get_date_type(calendar)
13271328
expected = [date_type(year, *args) for args in expected_month_day[::-1]]
1328-
with pytest.warns(DeprecationWarning):
1329-
result = cftime_range(
1330-
start="2001",
1331-
end="2000",
1332-
freq="-2ME",
1333-
calendar=calendar,
1334-
).values
1335-
np.testing.assert_equal(result, expected)
1329+
result = date_range(
1330+
start="2001", end="2000", freq="-2ME", calendar=calendar, use_cftime=True
1331+
).values
1332+
np.testing.assert_equal(result, expected)
13361333

13371334

13381335
@pytest.mark.parametrize(
@@ -1355,37 +1352,35 @@ def test_calendar_specific_month_end_negative_freq(
13551352
def test_calendar_year_length(
13561353
calendar: str, start: str, end: str, expected_number_of_days: int
13571354
) -> None:
1358-
with pytest.warns(DeprecationWarning):
1359-
result = cftime_range(start, end, freq="D", inclusive="left", calendar=calendar)
1360-
assert len(result) == expected_number_of_days
1355+
result = date_range(
1356+
start, end, freq="D", inclusive="left", calendar=calendar, use_cftime=True
1357+
)
1358+
assert len(result) == expected_number_of_days
13611359

13621360

13631361
@pytest.mark.parametrize("freq", ["YE", "ME", "D"])
1364-
def test_dayofweek_after_cftime_range(freq: str) -> None:
1365-
with pytest.warns(DeprecationWarning):
1366-
result = cftime_range("2000-02-01", periods=3, freq=freq).dayofweek
1367-
# TODO: remove once requiring pandas 2.2+
1368-
freq = _new_to_legacy_freq(freq)
1369-
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofweek
1370-
np.testing.assert_array_equal(result, expected)
1362+
def test_dayofweek_after_cftime(freq: str) -> None:
1363+
result = date_range("2000-02-01", periods=3, freq=freq, use_cftime=True).dayofweek
1364+
# TODO: remove once requiring pandas 2.2+
1365+
freq = _new_to_legacy_freq(freq)
1366+
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofweek
1367+
np.testing.assert_array_equal(result, expected)
13711368

13721369

13731370
@pytest.mark.parametrize("freq", ["YE", "ME", "D"])
1374-
def test_dayofyear_after_cftime_range(freq: str) -> None:
1375-
with pytest.warns(DeprecationWarning):
1376-
result = cftime_range("2000-02-01", periods=3, freq=freq).dayofyear
1377-
# TODO: remove once requiring pandas 2.2+
1378-
freq = _new_to_legacy_freq(freq)
1379-
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofyear
1380-
np.testing.assert_array_equal(result, expected)
1371+
def test_dayofyear_after_cftime(freq: str) -> None:
1372+
result = date_range("2000-02-01", periods=3, freq=freq, use_cftime=True).dayofyear
1373+
# TODO: remove once requiring pandas 2.2+
1374+
freq = _new_to_legacy_freq(freq)
1375+
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofyear
1376+
np.testing.assert_array_equal(result, expected)
13811377

13821378

13831379
def test_cftime_range_standard_calendar_refers_to_gregorian() -> None:
13841380
from cftime import DatetimeGregorian
13851381

1386-
with pytest.warns(DeprecationWarning):
1387-
(result,) = cftime_range("2000", periods=1)
1388-
assert isinstance(result, DatetimeGregorian)
1382+
(result,) = date_range("2000", periods=1, use_cftime=True)
1383+
assert isinstance(result, DatetimeGregorian)
13891384

13901385

13911386
@pytest.mark.parametrize(
@@ -1702,16 +1697,15 @@ def test_cftime_range_same_as_pandas(start, end, freq) -> None:
17021697
)
17031698
def test_cftime_range_no_freq(start, end, periods):
17041699
"""
1705-
Test whether cftime_range produces the same result as Pandas
1700+
Test whether date_range produces the same result as Pandas
17061701
when freq is not provided, but start, end and periods are.
17071702
"""
17081703
# Generate date ranges using cftime_range
1709-
with pytest.warns(DeprecationWarning):
1710-
cftimeindex = cftime_range(start=start, end=end, periods=periods)
1711-
result = cftimeindex.to_datetimeindex(time_unit="ns")
1712-
expected = pd.date_range(start=start, end=end, periods=periods)
1704+
cftimeindex = date_range(start=start, end=end, periods=periods, use_cftime=True)
1705+
result = cftimeindex.to_datetimeindex(time_unit="ns")
1706+
expected = pd.date_range(start=start, end=end, periods=periods)
17131707

1714-
np.testing.assert_array_equal(result, expected)
1708+
np.testing.assert_array_equal(result, expected)
17151709

17161710

17171711
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)