(timestamps, timezone=None)
| 2629 | |
| 2630 | |
| 2631 | def _check_datetime_components(timestamps, timezone=None): |
| 2632 | ts = pd.to_datetime(timestamps).tz_localize( |
| 2633 | "UTC").tz_convert(timezone).to_series() |
| 2634 | tsa = pa.array(ts, pa.timestamp("ns", tz=timezone)) |
| 2635 | |
| 2636 | subseconds = ((ts.dt.microsecond * 10 ** 3 + |
| 2637 | ts.dt.nanosecond) * 10 ** -9).round(9) |
| 2638 | iso_calendar_fields = [ |
| 2639 | pa.field('iso_year', pa.int64()), |
| 2640 | pa.field('iso_week', pa.int64()), |
| 2641 | pa.field('iso_day_of_week', pa.int64()) |
| 2642 | ] |
| 2643 | |
| 2644 | # Casting is required because pandas isocalendar returns int32 |
| 2645 | # while arrow isocalendar returns int64. |
| 2646 | iso_year = ts.dt.isocalendar()["year"].astype("int64") |
| 2647 | iso_week = ts.dt.isocalendar()["week"].astype("int64") |
| 2648 | iso_day = ts.dt.isocalendar()["day"].astype("int64") |
| 2649 | |
| 2650 | iso_calendar = pa.StructArray.from_arrays( |
| 2651 | [iso_year, iso_week, iso_day], |
| 2652 | fields=iso_calendar_fields) |
| 2653 | |
| 2654 | # Casting is required because pandas with 2.0.0 various numeric |
| 2655 | # date/time attributes have dtype int32 (previously int64) |
| 2656 | year = ts.dt.year.astype("int64") |
| 2657 | month = ts.dt.month.astype("int64") |
| 2658 | day = ts.dt.day.astype("int64") |
| 2659 | dayofweek = ts.dt.dayofweek.astype("int64") |
| 2660 | dayofyear = ts.dt.dayofyear.astype("int64") |
| 2661 | quarter = ts.dt.quarter.astype("int64") |
| 2662 | hour = ts.dt.hour.astype("int64") |
| 2663 | minute = ts.dt.minute.astype("int64") |
| 2664 | second = ts.dt.second.values.astype("int64") |
| 2665 | microsecond = ts.dt.microsecond.astype("int64") |
| 2666 | nanosecond = ts.dt.nanosecond.astype("int64") |
| 2667 | |
| 2668 | assert pc.year(tsa).equals(pa.array(year)) |
| 2669 | assert pc.is_leap_year(tsa).equals(pa.array(ts.dt.is_leap_year)) |
| 2670 | assert pc.month(tsa).equals(pa.array(month)) |
| 2671 | assert pc.day(tsa).equals(pa.array(day)) |
| 2672 | assert pc.day_of_week(tsa).equals(pa.array(dayofweek)) |
| 2673 | assert pc.day_of_year(tsa).equals(pa.array(dayofyear)) |
| 2674 | assert pc.iso_year(tsa).equals(pa.array(iso_year)) |
| 2675 | assert pc.iso_week(tsa).equals(pa.array(iso_week)) |
| 2676 | assert pc.iso_calendar(tsa).equals(iso_calendar) |
| 2677 | assert pc.quarter(tsa).equals(pa.array(quarter)) |
| 2678 | assert pc.hour(tsa).equals(pa.array(hour)) |
| 2679 | assert pc.minute(tsa).equals(pa.array(minute)) |
| 2680 | assert pc.second(tsa).equals(pa.array(second)) |
| 2681 | assert pc.millisecond(tsa).equals(pa.array(microsecond // 10 ** 3)) |
| 2682 | assert pc.microsecond(tsa).equals(pa.array(microsecond % 10 ** 3)) |
| 2683 | assert pc.nanosecond(tsa).equals(pa.array(nanosecond)) |
| 2684 | assert pc.subsecond(tsa).equals(pa.array(subseconds)) |
| 2685 | assert pc.local_timestamp(tsa).equals(pa.array(ts.dt.tz_localize(None))) |
| 2686 | |
| 2687 | if ts.dt.tz: |
| 2688 | if ts.dt.tz is datetime.timezone.utc: |
no test coverage detected