|
29 | 29 |
|
30 | 30 | import pandas as pd |
31 | 31 | import pandas._testing as tm |
| 32 | +from pandas.util.version import Version |
32 | 33 |
|
33 | 34 |
|
34 | 35 | @pytest.mark.single_cpu |
@@ -589,6 +590,97 @@ def test_zoneinfo_from_file_without_key_ambiguous_infer(): |
589 | 590 | ) |
590 | 591 |
|
591 | 592 |
|
| 593 | +_dateutil_lt_3 = Version(dateutil.__version__) < Version("3") # type: ignore[attr-defined] |
| 594 | +_skip_if_dateutil_lt_3 = pytest.mark.skipif( |
| 595 | + _dateutil_lt_3, reason="tzfile key added in 3.0" |
| 596 | +) |
| 597 | + |
| 598 | + |
| 599 | +@pytest.mark.parametrize( |
| 600 | + "make_tz", |
| 601 | + [ |
| 602 | + lambda: dateutil.tz.gettz("US/Eastern"), |
| 603 | + # no key, so no zoneinfo twin |
| 604 | + pytest.param( |
| 605 | + lambda: dateutil.tz.tzfile(io.BytesIO(_tzdata_bytes("US/Eastern"))), |
| 606 | + marks=pytest.mark.skipif( |
| 607 | + _dateutil_lt_3, |
| 608 | + reason="dateutil<3 reads only the TZif v1 block, which is empty " |
| 609 | + "in slim tzdata files", |
| 610 | + ), |
| 611 | + ), |
| 612 | + # dateutil accepts this key but zoneinfo does not |
| 613 | + pytest.param( |
| 614 | + lambda: dateutil.tz.gettz("America/New York"), |
| 615 | + marks=pytest.mark.skipif( |
| 616 | + _dateutil_lt_3 and is_platform_windows(), |
| 617 | + reason="dateutil<3 resolves spaced keys only on a system TZPATH", |
| 618 | + ), |
| 619 | + ), |
| 620 | + # a key naming a different zone than the data |
| 621 | + pytest.param( |
| 622 | + lambda: dateutil.tz.tzfile( |
| 623 | + io.BytesIO(_tzdata_bytes("US/Eastern")), |
| 624 | + key="Europe/Paris", # type: ignore[call-arg] |
| 625 | + ), |
| 626 | + marks=_skip_if_dateutil_lt_3, |
| 627 | + ), |
| 628 | + ], |
| 629 | +) |
| 630 | +@pytest.mark.parametrize("unit", ["s", "ns"]) |
| 631 | +def test_dateutil_tzfile_matches_zoneinfo(make_tz, unit): |
| 632 | + # GH#71459 dateutil>=3 tzfile no longer has the attributes the dateutil |
| 633 | + # path reads, so every DST zone raised AttributeError |
| 634 | + tz = make_tz() |
| 635 | + expected_tz = zoneinfo.ZoneInfo("US/Eastern") |
| 636 | + |
| 637 | + naive = pd.DatetimeIndex( |
| 638 | + ["2025-03-09 02:30", "2025-11-02 01:30", "2025-07-01"] |
| 639 | + ).as_unit(unit) |
| 640 | + kwargs = {"ambiguous": True, "nonexistent": "shift_forward"} |
| 641 | + result = naive.tz_localize(tz, **kwargs) |
| 642 | + expected = naive.tz_localize(expected_tz, **kwargs) |
| 643 | + tm.assert_numpy_array_equal(result.asi8, expected.asi8) |
| 644 | + |
| 645 | + utc = pd.date_range("2025-01-01", "2025-12-31", freq="7h", tz="UTC", unit=unit) |
| 646 | + assert [ts.utcoffset() for ts in utc.tz_convert(tz)] == [ |
| 647 | + ts.utcoffset() for ts in utc.tz_convert(expected_tz) |
| 648 | + ] |
| 649 | + |
| 650 | + # dateutil<3 has no rules past 2037, so check far-future dates against tz |
| 651 | + for wall in [datetime(2025, 6, 15, 12), datetime(2150, 7, 1)]: |
| 652 | + ts = pd.Timestamp(wall).as_unit(unit) |
| 653 | + assert ts.tz_localize(tz).timestamp() == wall.replace(tzinfo=tz).timestamp() |
| 654 | + |
| 655 | + |
| 656 | +@_skip_if_dateutil_lt_3 |
| 657 | +def test_dateutil_tzfile_fast_path_detection(): |
| 658 | + # GH#71459 is_fixed_offset needs the zoneinfo twin, which dateutil>=3 |
| 659 | + # zones get only from a key that matches their data |
| 660 | + data = io.BytesIO(_tzdata_bytes("US/Eastern")) |
| 661 | + assert timezones.is_fixed_offset(dateutil.tz.gettz("Etc/GMT+5")) |
| 662 | + assert not timezones.is_fixed_offset(dateutil.tz.tzfile(data, key="Etc/GMT+5")) |
| 663 | + |
| 664 | + |
| 665 | +@_skip_if_dateutil_lt_3 |
| 666 | +def test_dateutil_tzfile_get_timezone_uses_key(): |
| 667 | + # GH#71459 _filename is a machine-specific path, so Timestamp repr and |
| 668 | + # serialized zones use the key instead, when it matches the data |
| 669 | + tz = dateutil.tz.gettz("US/Eastern") |
| 670 | + assert timezones.get_timezone(tz) == "dateutil/US/Eastern" |
| 671 | + ts = pd.Timestamp("2025-01-01", tz=tz) |
| 672 | + assert "tz='dateutil/US/Eastern'" in repr(ts) |
| 673 | + |
| 674 | + # a key that doesn't match the data is ignored; filename= is passed |
| 675 | + # because _filename otherwise defaults to the key |
| 676 | + mislabeled = dateutil.tz.tzfile( |
| 677 | + io.BytesIO(_tzdata_bytes("US/Eastern")), |
| 678 | + filename="mislabeled", |
| 679 | + key="Europe/Paris", |
| 680 | + ) |
| 681 | + assert timezones.get_timezone(mislabeled) == "dateutil/mislabeled" |
| 682 | + |
| 683 | + |
592 | 684 | def test_zoneinfo_fast_path_detection(): |
593 | 685 | # GH#64379 is_fixed_offset needs the pure-python twin, so it shows which |
594 | 686 | # ZoneInfo objects keep the cached transition fast path. |
|
0 commit comments