|
51 | 51 |
|
52 | 52 | import numpy as np |
53 | 53 | import pandas as pd |
54 | | -from packaging.version import Version |
55 | 54 |
|
56 | 55 | from xarray.coding.cftimeindex import CFTimeIndex |
57 | 56 | from xarray.coding.times import ( |
@@ -782,7 +781,7 @@ def to_offset( |
782 | 781 | if isinstance(freq, timedelta | pd.Timedelta): |
783 | 782 | return delta_to_tick(freq) |
784 | 783 | if isinstance(freq, pd.DateOffset): |
785 | | - freq = _legacy_to_new_freq(freq.freqstr) |
| 784 | + freq = freq.freqstr |
786 | 785 |
|
787 | 786 | match = re.match(_PATTERN, freq) |
788 | 787 | if match is None: |
@@ -1430,8 +1429,7 @@ def date_range( |
1430 | 1429 | start=start, |
1431 | 1430 | end=end, |
1432 | 1431 | periods=periods, |
1433 | | - # TODO remove translation once requiring pandas >= 2.2 |
1434 | | - freq=_new_to_legacy_freq(freq), |
| 1432 | + freq=freq, |
1435 | 1433 | tz=tz, |
1436 | 1434 | normalize=normalize, |
1437 | 1435 | name=name, |
@@ -1460,96 +1458,6 @@ def date_range( |
1460 | 1458 | ) |
1461 | 1459 |
|
1462 | 1460 |
|
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 | | - |
1553 | 1461 | def date_range_like(source, calendar, use_cftime=None): |
1554 | 1462 | """Generate a datetime array with the same frequency, start and end as |
1555 | 1463 | another one, but in a different calendar. |
@@ -1593,9 +1501,6 @@ def date_range_like(source, calendar, use_cftime=None): |
1593 | 1501 | "`date_range_like` was unable to generate a range as the source frequency was not inferable." |
1594 | 1502 | ) |
1595 | 1503 |
|
1596 | | - # TODO remove once requiring pandas >= 2.2 |
1597 | | - freq = _legacy_to_new_freq(freq) |
1598 | | - |
1599 | 1504 | use_cftime = _should_cftime_be_used(source, calendar, use_cftime) |
1600 | 1505 |
|
1601 | 1506 | source_start = source.values.min() |
|
0 commit comments