MCPcopy Create free account
hub / github.com/apache/arrow / test_assume_timezone

Function test_assume_timezone

python/pyarrow/tests/test_compute.py:2759–2848  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2757@pytest.mark.pandas
2758@pytest.mark.timezone_data
2759def test_assume_timezone():
2760 ts_type = pa.timestamp("ns")
2761 timestamps = pd.to_datetime(["1970-01-01T00:00:59.123456789",
2762 "2000-02-29T23:23:23.999999999",
2763 "2033-05-18T03:33:20.000000000",
2764 "2020-01-01T01:05:05.001",
2765 "2019-12-31T02:10:10.002",
2766 "2019-12-30T03:15:15.003",
2767 "2009-12-31T04:20:20.004132",
2768 "2010-01-01T05:25:25.005321",
2769 "2010-01-03T06:30:30.006163",
2770 "2010-01-04T07:35:35.0",
2771 "2006-01-01T08:40:40.0",
2772 "2005-12-31T09:45:45.0",
2773 "2008-12-28T00:00:00.0",
2774 "2008-12-29T00:00:00.0",
2775 "2012-01-01T01:02:03.0"])
2776 nonexistent = pd.to_datetime(["2015-03-29 02:30:00",
2777 "2015-03-29 03:30:00"])
2778 ambiguous = pd.to_datetime(["2018-10-28 01:20:00",
2779 "2018-10-28 02:36:00",
2780 "2018-10-28 03:46:00"])
2781 ambiguous_array = pa.array(ambiguous, type=ts_type)
2782 nonexistent_array = pa.array(nonexistent, type=ts_type)
2783
2784 for timezone in ["UTC", "America/Chicago", "Asia/Kolkata"]:
2785 options = pc.AssumeTimezoneOptions(timezone)
2786 ta = pa.array(timestamps, type=ts_type)
2787 expected = timestamps.tz_localize(timezone)
2788 result = pc.assume_timezone(ta, options=options)
2789 assert result.equals(pa.array(expected))
2790 result = pc.assume_timezone(ta, timezone) # Positional option
2791 assert result.equals(pa.array(expected))
2792
2793 ta_zoned = pa.array(timestamps, type=pa.timestamp("ns", timezone))
2794 with pytest.raises(pa.ArrowInvalid, match="already have a timezone:"):
2795 pc.assume_timezone(ta_zoned, options=options)
2796
2797 invalid_options = pc.AssumeTimezoneOptions("Europe/Brusselsss")
2798 with pytest.raises(ValueError,
2799 match="not found in timezone database|"
2800 "unable to locate time_zone"):
2801 pc.assume_timezone(ta, options=invalid_options)
2802
2803 timezone = "Europe/Brussels"
2804
2805 options_nonexistent_raise = pc.AssumeTimezoneOptions(timezone)
2806 options_nonexistent_earliest = pc.AssumeTimezoneOptions(
2807 timezone, ambiguous="raise", nonexistent="earliest")
2808 options_nonexistent_latest = pc.AssumeTimezoneOptions(
2809 timezone, ambiguous="raise", nonexistent="latest")
2810
2811 with pytest.raises(ValueError,
2812 match="Timestamp doesn't exist in "
2813 f"timezone '{timezone}'"):
2814 pc.assume_timezone(nonexistent_array,
2815 options=options_nonexistent_raise)
2816

Callers

nothing calls this directly

Calls 4

to_datetimeMethod · 0.80
AssumeTimezoneOptionsMethod · 0.80
equalsMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected