()
| 2602 | @pytest.mark.pandas |
| 2603 | @pytest.mark.timezone_data |
| 2604 | def test_assume_timezone(): |
| 2605 | ts_type = pa.timestamp("ns") |
| 2606 | timestamps = pd.to_datetime(["1970-01-01T00:00:59.123456789", |
| 2607 | "2000-02-29T23:23:23.999999999", |
| 2608 | "2033-05-18T03:33:20.000000000", |
| 2609 | "2020-01-01T01:05:05.001", |
| 2610 | "2019-12-31T02:10:10.002", |
| 2611 | "2019-12-30T03:15:15.003", |
| 2612 | "2009-12-31T04:20:20.004132", |
| 2613 | "2010-01-01T05:25:25.005321", |
| 2614 | "2010-01-03T06:30:30.006163", |
| 2615 | "2010-01-04T07:35:35.0", |
| 2616 | "2006-01-01T08:40:40.0", |
| 2617 | "2005-12-31T09:45:45.0", |
| 2618 | "2008-12-28T00:00:00.0", |
| 2619 | "2008-12-29T00:00:00.0", |
| 2620 | "2012-01-01T01:02:03.0"]) |
| 2621 | nonexistent = pd.to_datetime(["2015-03-29 02:30:00", |
| 2622 | "2015-03-29 03:30:00"]) |
| 2623 | ambiguous = pd.to_datetime(["2018-10-28 01:20:00", |
| 2624 | "2018-10-28 02:36:00", |
| 2625 | "2018-10-28 03:46:00"]) |
| 2626 | ambiguous_array = pa.array(ambiguous, type=ts_type) |
| 2627 | nonexistent_array = pa.array(nonexistent, type=ts_type) |
| 2628 | |
| 2629 | for timezone in ["UTC", "America/Chicago", "Asia/Kolkata"]: |
| 2630 | options = pc.AssumeTimezoneOptions(timezone) |
| 2631 | ta = pa.array(timestamps, type=ts_type) |
| 2632 | expected = timestamps.tz_localize(timezone) |
| 2633 | result = pc.assume_timezone(ta, options=options) |
| 2634 | assert result.equals(pa.array(expected)) |
| 2635 | result = pc.assume_timezone(ta, timezone) # Positional option |
| 2636 | assert result.equals(pa.array(expected)) |
| 2637 | |
| 2638 | ta_zoned = pa.array(timestamps, type=pa.timestamp("ns", timezone)) |
| 2639 | with pytest.raises(pa.ArrowInvalid, match="already have a timezone:"): |
| 2640 | pc.assume_timezone(ta_zoned, options=options) |
| 2641 | |
| 2642 | invalid_options = pc.AssumeTimezoneOptions("Europe/Brusselsss") |
| 2643 | with pytest.raises(ValueError, |
| 2644 | match="not found in timezone database|" |
| 2645 | "unable to locate time_zone"): |
| 2646 | pc.assume_timezone(ta, options=invalid_options) |
| 2647 | |
| 2648 | timezone = "Europe/Brussels" |
| 2649 | |
| 2650 | options_nonexistent_raise = pc.AssumeTimezoneOptions(timezone) |
| 2651 | options_nonexistent_earliest = pc.AssumeTimezoneOptions( |
| 2652 | timezone, ambiguous="raise", nonexistent="earliest") |
| 2653 | options_nonexistent_latest = pc.AssumeTimezoneOptions( |
| 2654 | timezone, ambiguous="raise", nonexistent="latest") |
| 2655 | |
| 2656 | with pytest.raises(ValueError, |
| 2657 | match="Timestamp doesn't exist in " |
| 2658 | f"timezone '{timezone}'"): |
| 2659 | pc.assume_timezone(nonexistent_array, |
| 2660 | options=options_nonexistent_raise) |
| 2661 |
nothing calls this directly
no test coverage detected