(freq: T_FreqStr)
| 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 | def date_range_like(source, calendar, use_cftime=None): |
searching dependent graphs…