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

Function test_assume_timezone

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

Source from the content-addressed store, hash-verified

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

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