Skip to content

Commit 66e7d83

Browse files
MAINT: remove pandas 2.2 compat (incl. infer_freq) (#11430)
* MAINT: remove pandas 2.2 compat (incl. infer_freq) * rm timestamp_as_unit * ignore type warnings --------- Co-authored-by: Spencer Clark <[email protected]>
1 parent 1ceccee commit 66e7d83

9 files changed

Lines changed: 13 additions & 253 deletions

File tree

‎xarray/coding/cftime_offsets.py‎

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252
import numpy as np
5353
import pandas as pd
54-
from packaging.version import Version
5554

5655
from xarray.coding.cftimeindex import CFTimeIndex
5756
from xarray.coding.times import (
@@ -782,7 +781,7 @@ def to_offset(
782781
if isinstance(freq, timedelta | pd.Timedelta):
783782
return delta_to_tick(freq)
784783
if isinstance(freq, pd.DateOffset):
785-
freq = _legacy_to_new_freq(freq.freqstr)
784+
freq = freq.freqstr
786785

787786
match = re.match(_PATTERN, freq)
788787
if match is None:
@@ -1430,8 +1429,7 @@ def date_range(
14301429
start=start,
14311430
end=end,
14321431
periods=periods,
1433-
# TODO remove translation once requiring pandas >= 2.2
1434-
freq=_new_to_legacy_freq(freq),
1432+
freq=freq,
14351433
tz=tz,
14361434
normalize=normalize,
14371435
name=name,
@@ -1460,96 +1458,6 @@ def date_range(
14601458
)
14611459

14621460

1463-
def _new_to_legacy_freq(freq):
1464-
# xarray will now always return "ME" and "QE" for MonthEnd and QuarterEnd
1465-
# frequencies, but older versions of pandas do not support these as
1466-
# frequency strings. Until xarray's minimum pandas version is 2.2 or above,
1467-
# we add logic to continue using the deprecated "M" and "Q" frequency
1468-
# strings in these circumstances.
1469-
1470-
# NOTE: other conversions ("h" -> "H", ..., "ns" -> "N") not required
1471-
1472-
# TODO: remove once requiring pandas >= 2.2
1473-
if not freq or Version(pd.__version__) >= Version("2.2"):
1474-
return freq
1475-
1476-
try:
1477-
freq_as_offset = to_offset(freq)
1478-
except ValueError:
1479-
# freq may be valid in pandas but not in xarray
1480-
return freq
1481-
1482-
if isinstance(freq_as_offset, MonthEnd) and "ME" in freq:
1483-
freq = freq.replace("ME", "M")
1484-
elif isinstance(freq_as_offset, QuarterEnd) and "QE" in freq:
1485-
freq = freq.replace("QE", "Q")
1486-
elif isinstance(freq_as_offset, YearBegin) and "YS" in freq:
1487-
freq = freq.replace("YS", "AS")
1488-
elif isinstance(freq_as_offset, YearEnd):
1489-
# testing for "Y" is required as this was valid in xarray 2023.11 - 2024.01
1490-
if "Y-" in freq:
1491-
# Check for and replace "Y-" instead of just "Y" to prevent
1492-
# corrupting anchored offsets that contain "Y" in the month
1493-
# abbreviation, e.g. "Y-MAY" -> "A-MAY".
1494-
freq = freq.replace("Y-", "A-")
1495-
elif "YE-" in freq:
1496-
freq = freq.replace("YE-", "A-")
1497-
elif "A-" not in freq and freq.endswith("Y"):
1498-
freq = freq.replace("Y", "A")
1499-
elif freq.endswith("YE"):
1500-
freq = freq.replace("YE", "A")
1501-
1502-
return freq
1503-
1504-
1505-
def _legacy_to_new_freq(freq: T_FreqStr) -> T_FreqStr:
1506-
# to avoid internal deprecation warnings when freq is determined using pandas < 2.2
1507-
1508-
# TODO: remove once requiring pandas >= 2.2
1509-
1510-
if not freq or Version(pd.__version__) >= Version("2.2"):
1511-
return freq
1512-
1513-
try:
1514-
freq_as_offset = to_offset(freq, warn=False)
1515-
except ValueError:
1516-
# freq may be valid in pandas but not in xarray
1517-
return freq
1518-
1519-
if isinstance(freq_as_offset, MonthEnd) and "ME" not in freq:
1520-
freq = freq.replace("M", "ME")
1521-
elif isinstance(freq_as_offset, QuarterEnd) and "QE" not in freq:
1522-
freq = freq.replace("Q", "QE")
1523-
elif isinstance(freq_as_offset, YearBegin) and "YS" not in freq:
1524-
freq = freq.replace("AS", "YS")
1525-
elif isinstance(freq_as_offset, YearEnd):
1526-
if "A-" in freq:
1527-
# Check for and replace "A-" instead of just "A" to prevent
1528-
# corrupting anchored offsets that contain "Y" in the month
1529-
# abbreviation, e.g. "A-MAY" -> "YE-MAY".
1530-
freq = freq.replace("A-", "YE-")
1531-
elif "Y-" in freq:
1532-
freq = freq.replace("Y-", "YE-")
1533-
elif freq.endswith("A"):
1534-
# the "A-MAY" case is already handled above
1535-
freq = freq.replace("A", "YE")
1536-
elif "YE" not in freq and freq.endswith("Y"):
1537-
# the "Y-MAY" case is already handled above
1538-
freq = freq.replace("Y", "YE")
1539-
elif isinstance(freq_as_offset, Hour):
1540-
freq = freq.replace("H", "h")
1541-
elif isinstance(freq_as_offset, Minute):
1542-
freq = freq.replace("T", "min")
1543-
elif isinstance(freq_as_offset, Second):
1544-
freq = freq.replace("S", "s")
1545-
elif isinstance(freq_as_offset, Millisecond):
1546-
freq = freq.replace("L", "ms")
1547-
elif isinstance(freq_as_offset, Microsecond):
1548-
freq = freq.replace("U", "us")
1549-
1550-
return freq
1551-
1552-
15531461
def date_range_like(source, calendar, use_cftime=None):
15541462
"""Generate a datetime array with the same frequency, start and end as
15551463
another one, but in a different calendar.
@@ -1593,9 +1501,6 @@ def date_range_like(source, calendar, use_cftime=None):
15931501
"`date_range_like` was unable to generate a range as the source frequency was not inferable."
15941502
)
15951503

1596-
# TODO remove once requiring pandas >= 2.2
1597-
freq = _legacy_to_new_freq(freq)
1598-
15991504
use_cftime = _should_cftime_be_used(source, calendar, use_cftime)
16001505

16011506
source_start = source.values.min()

‎xarray/coding/frequencies.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import numpy as np
4646
import pandas as pd
4747

48-
from xarray.coding.cftime_offsets import _MONTH_ABBREVIATIONS, _legacy_to_new_freq
48+
from xarray.coding.cftime_offsets import _MONTH_ABBREVIATIONS
4949
from xarray.coding.cftimeindex import CFTimeIndex
5050
from xarray.core.common import _contains_datetime_like_objects
5151
from xarray.core.dtypes import _is_numpy_subdtype
@@ -101,7 +101,7 @@ def infer_freq(index):
101101
inferer = _CFTimeFrequencyInferer(index)
102102
return inferer.get_freq()
103103

104-
return _legacy_to_new_freq(pd.infer_freq(index))
104+
return pd.infer_freq(index)
105105

106106

107107
class _CFTimeFrequencyInferer: # (pd.tseries.frequencies._FrequencyInferer):

‎xarray/coding/times.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
unpack_for_decoding,
2222
unpack_for_encoding,
2323
)
24-
from xarray.compat.pdcompat import default_precision_timestamp, timestamp_as_unit
24+
from xarray.compat.pdcompat import default_precision_timestamp
2525
from xarray.core import indexing
2626
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like
2727
from xarray.core.duck_array_ops import array_all, asarray, ravel, reshape
@@ -419,7 +419,7 @@ def _check_date_for_units_since_refdate(
419419
return pd.Timestamp("NaT")
420420
# this will raise on overflow if ref_date + delta can't be represented in
421421
# the current ref_date resolution
422-
return timestamp_as_unit(ref_date + delta, ref_date.unit)
422+
return (ref_date + delta).as_unit(ref_date.unit)
423423

424424

425425
def _check_timedelta_range(value, data_unit, time_unit):
@@ -437,7 +437,7 @@ def _align_reference_date_and_unit(
437437
if np.timedelta64(1, ref_date.unit) > np.timedelta64(1, unit):
438438
# this will raise accordingly
439439
# if data can't be represented in the higher resolution
440-
return timestamp_as_unit(ref_date, cast(PDDatetimeUnitOptions, unit))
440+
return ref_date.as_unit(cast(PDDatetimeUnitOptions, unit))
441441
return ref_date
442442

443443

‎xarray/compat/pdcompat.py‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939

4040
import pandas as pd
4141

42-
from xarray.core.types import PDDatetimeUnitOptions
43-
4442

4543
def count_not_none(*args) -> int:
4644
"""Compute the number of non-None arguments.
@@ -73,26 +71,12 @@ def __repr__(self) -> str:
7371
NoDefault = Literal[_NoDefault.no_default] # For typing following pandas
7472

7573

76-
def timestamp_as_unit(date: pd.Timestamp, unit: PDDatetimeUnitOptions) -> pd.Timestamp:
77-
"""Convert the underlying int64 representation to the given unit.
78-
79-
Compatibility function for pandas issue where "as_unit" is not defined
80-
for pandas.Timestamp in pandas versions < 2.2. Can be removed minimum
81-
pandas version is >= 2.2.
82-
"""
83-
if hasattr(date, "as_unit"):
84-
date = date.as_unit(unit)
85-
elif hasattr(date, "_as_unit"):
86-
date = date._as_unit(unit)
87-
return date
88-
89-
9074
def default_precision_timestamp(*args, **kwargs) -> pd.Timestamp:
9175
"""Return a Timestamp object with the default precision.
9276
9377
Xarray default is "ns".
9478
"""
9579
dt = pd.Timestamp(*args, **kwargs)
9680
if dt.unit != "ns":
97-
dt = timestamp_as_unit(dt, "ns")
81+
dt = dt.as_unit("ns")
9882
return dt

‎xarray/groupers.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import pandas as pd
2323
from numpy.typing import ArrayLike
2424

25-
from xarray.coding.cftime_offsets import BaseCFTimeOffset, _new_to_legacy_freq
25+
from xarray.coding.cftime_offsets import BaseCFTimeOffset
2626
from xarray.coding.cftimeindex import CFTimeIndex
2727
from xarray.compat.toolzcompat import sliding_window
2828
from xarray.computation.apply_ufunc import apply_ufunc
@@ -540,9 +540,8 @@ def _init_properties(self, group: T_Group) -> None:
540540
"when resampling a 'CFTimeIndex'"
541541
)
542542

543-
self.index_grouper = pd.Grouper(
544-
# TODO remove once requiring pandas >= 2.2
545-
freq=_new_to_legacy_freq(self.freq),
543+
self.index_grouper = pd.Grouper( # type:ignore[misc]
544+
freq=self.freq, # type:ignore[arg-type]
546545
closed=self.closed,
547546
label=self.label,
548547
origin=self.origin,

‎xarray/tests/__init__.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ def get_dask_chunkmanager():
189189
has_numexpr, requires_numexpr = _importorskip("numexpr")
190190
has_flox, requires_flox = _importorskip("flox")
191191
has_netcdf, requires_netcdf = _importorskip("netcdf")
192-
has_pandas_ge_2_2, requires_pandas_ge_2_2 = _importorskip("pandas", "2.2")
193192
has_pandas_3, requires_pandas_3 = _importorskip("pandas", "3.0.0")
194193

195194

‎xarray/tests/test_cftime_offsets.py‎

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
Tick,
2626
YearBegin,
2727
YearEnd,
28-
_legacy_to_new_freq,
29-
_new_to_legacy_freq,
3028
cftime_range,
3129
date_range,
3230
date_range_like,
@@ -40,7 +38,6 @@
4038
_CFTIME_CALENDARS,
4139
assert_no_warnings,
4240
has_cftime,
43-
has_pandas_ge_2_2,
4441
requires_cftime,
4542
requires_pandas_3,
4643
)
@@ -1381,17 +1378,13 @@ def test_calendar_year_length(
13811378
@pytest.mark.parametrize("freq", ["YE", "ME", "D"])
13821379
def test_dayofweek_after_cftime(freq: str) -> None:
13831380
result = date_range("2000-02-01", periods=3, freq=freq, use_cftime=True).day_of_week
1384-
# TODO: remove once requiring pandas 2.2+
1385-
freq = _new_to_legacy_freq(freq)
13861381
expected = pd.date_range("2000-02-01", periods=3, freq=freq).day_of_week
13871382
np.testing.assert_array_equal(result, expected)
13881383

13891384

13901385
@pytest.mark.parametrize("freq", ["YE", "ME", "D"])
13911386
def test_dayofyear_after_cftime(freq: str) -> None:
13921387
result = date_range("2000-02-01", periods=3, freq=freq, use_cftime=True).day_of_year
1393-
# TODO: remove once requiring pandas 2.2+
1394-
freq = _new_to_legacy_freq(freq)
13951388
expected = pd.date_range("2000-02-01", periods=3, freq=freq).day_of_year
13961389
np.testing.assert_array_equal(result, expected)
13971390

@@ -1573,114 +1566,6 @@ def test_to_offset_deprecation_warning(freq):
15731566
to_offset(freq)
15741567

15751568

1576-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only relevant for pandas lt 2.2")
1577-
@pytest.mark.parametrize(
1578-
"freq, expected",
1579-
(
1580-
["Y", "YE"],
1581-
["A", "YE"],
1582-
["Q", "QE"],
1583-
["M", "ME"],
1584-
["AS", "YS"],
1585-
["YE", "YE"],
1586-
["QE", "QE"],
1587-
["ME", "ME"],
1588-
["YS", "YS"],
1589-
),
1590-
)
1591-
@pytest.mark.parametrize("n", ("", "2"))
1592-
def test_legacy_to_new_freq(freq, expected, n):
1593-
freq = f"{n}{freq}"
1594-
result = _legacy_to_new_freq(freq)
1595-
1596-
expected = f"{n}{expected}"
1597-
1598-
assert result == expected
1599-
1600-
1601-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only relevant for pandas lt 2.2")
1602-
@pytest.mark.parametrize("year_alias", ("YE", "Y", "A"))
1603-
@pytest.mark.parametrize("n", ("", "2"))
1604-
def test_legacy_to_new_freq_anchored(year_alias, n):
1605-
for month in _MONTH_ABBREVIATIONS.values():
1606-
freq = f"{n}{year_alias}-{month}"
1607-
result = _legacy_to_new_freq(freq)
1608-
1609-
expected = f"{n}YE-{month}"
1610-
1611-
assert result == expected
1612-
1613-
1614-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only relevant for pandas lt 2.2")
1615-
@pytest.mark.filterwarnings("ignore:'[AY]' is deprecated")
1616-
@pytest.mark.parametrize(
1617-
"freq, expected",
1618-
(["A", "A"], ["YE", "A"], ["Y", "A"], ["QE", "Q"], ["ME", "M"], ["YS", "AS"]),
1619-
)
1620-
@pytest.mark.parametrize("n", ("", "2"))
1621-
def test_new_to_legacy_freq(freq, expected, n):
1622-
freq = f"{n}{freq}"
1623-
result = _new_to_legacy_freq(freq)
1624-
1625-
expected = f"{n}{expected}"
1626-
1627-
assert result == expected
1628-
1629-
1630-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only relevant for pandas lt 2.2")
1631-
@pytest.mark.filterwarnings("ignore:'[AY]-.{3}' is deprecated")
1632-
@pytest.mark.parametrize("year_alias", ("A", "Y", "YE"))
1633-
@pytest.mark.parametrize("n", ("", "2"))
1634-
def test_new_to_legacy_freq_anchored(year_alias, n):
1635-
for month in _MONTH_ABBREVIATIONS.values():
1636-
freq = f"{n}{year_alias}-{month}"
1637-
result = _new_to_legacy_freq(freq)
1638-
1639-
expected = f"{n}A-{month}"
1640-
1641-
assert result == expected
1642-
1643-
1644-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only for pandas lt 2.2")
1645-
@pytest.mark.parametrize(
1646-
"freq, expected",
1647-
(
1648-
# pandas-only freq strings are passed through
1649-
("BH", "BH"),
1650-
("CBH", "CBH"),
1651-
("N", "N"),
1652-
),
1653-
)
1654-
def test_legacy_to_new_freq_pd_freq_passthrough(freq, expected):
1655-
result = _legacy_to_new_freq(freq)
1656-
assert result == expected
1657-
1658-
1659-
@pytest.mark.filterwarnings("ignore:'.' is deprecated ")
1660-
@pytest.mark.skipif(has_pandas_ge_2_2, reason="only for pandas lt 2.2")
1661-
@pytest.mark.parametrize(
1662-
"freq, expected",
1663-
(
1664-
# these are each valid in pandas lt 2.2
1665-
("T", "T"),
1666-
("min", "min"),
1667-
("S", "S"),
1668-
("s", "s"),
1669-
("L", "L"),
1670-
("ms", "ms"),
1671-
("U", "U"),
1672-
("us", "us"),
1673-
# pandas-only freq strings are passed through
1674-
("bh", "bh"),
1675-
("cbh", "cbh"),
1676-
("ns", "ns"),
1677-
),
1678-
)
1679-
def test_new_to_legacy_freq_pd_freq_passthrough(freq, expected):
1680-
result = _new_to_legacy_freq(freq)
1681-
assert result == expected
1682-
1683-
16841569
@pytest.mark.filterwarnings("ignore:Converting a CFTimeIndex with:")
16851570
@pytest.mark.parametrize("start", ("2000", "2001"))
16861571
@pytest.mark.parametrize("end", ("2000", "2001"))

0 commit comments

Comments
 (0)