(freq)
| 1461 | |
| 1462 | |
| 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: |
searching dependent graphs…