(timestamps, timezone=None)
| 2612 | |
| 2613 | |
| 2614 | def _check_datetime_components(timestamps, timezone=None): |
| 2615 | from pyarrow.vendored.version import Version |
| 2616 | |
| 2617 | ts = pd.to_datetime(timestamps).tz_localize( |
| 2618 | "UTC").tz_convert(timezone).to_series() |
| 2619 | tsa = pa.array(ts, pa.timestamp("ns", tz=timezone)) |
| 2620 | |
| 2621 | subseconds = ((ts.dt.microsecond * 10 ** 3 + |
| 2622 | ts.dt.nanosecond) * 10 ** -9).round(9) |
| 2623 | iso_calendar_fields = [ |
| 2624 | pa.field('iso_year', pa.int64()), |
| 2625 | pa.field('iso_week', pa.int64()), |
| 2626 | pa.field('iso_day_of_week', pa.int64()) |
| 2627 | ] |
| 2628 | |
| 2629 | if Version(pd.__version__) < Version("1.1.0"): |
| 2630 | # https://github.com/pandas-dev/pandas/issues/33206 |
| 2631 | iso_year = ts.map(lambda x: x.isocalendar()[0]).astype("int64") |
| 2632 | iso_week = ts.map(lambda x: x.isocalendar()[1]).astype("int64") |
| 2633 | iso_day = ts.map(lambda x: x.isocalendar()[2]).astype("int64") |
| 2634 | else: |
| 2635 | # Casting is required because pandas isocalendar returns int32 |
| 2636 | # while arrow isocalendar returns int64. |
| 2637 | iso_year = ts.dt.isocalendar()["year"].astype("int64") |
| 2638 | iso_week = ts.dt.isocalendar()["week"].astype("int64") |
| 2639 | iso_day = ts.dt.isocalendar()["day"].astype("int64") |
| 2640 | |
| 2641 | iso_calendar = pa.StructArray.from_arrays( |
| 2642 | [iso_year, iso_week, iso_day], |
| 2643 | fields=iso_calendar_fields) |
| 2644 | |
| 2645 | # Casting is required because pandas with 2.0.0 various numeric |
| 2646 | # date/time attributes have dtype int32 (previously int64) |
| 2647 | year = ts.dt.year.astype("int64") |
| 2648 | month = ts.dt.month.astype("int64") |
| 2649 | day = ts.dt.day.astype("int64") |
| 2650 | dayofweek = ts.dt.dayofweek.astype("int64") |
| 2651 | dayofyear = ts.dt.dayofyear.astype("int64") |
| 2652 | quarter = ts.dt.quarter.astype("int64") |
| 2653 | hour = ts.dt.hour.astype("int64") |
| 2654 | minute = ts.dt.minute.astype("int64") |
| 2655 | second = ts.dt.second.values.astype("int64") |
| 2656 | microsecond = ts.dt.microsecond.astype("int64") |
| 2657 | nanosecond = ts.dt.nanosecond.astype("int64") |
| 2658 | |
| 2659 | assert pc.year(tsa).equals(pa.array(year)) |
| 2660 | assert pc.is_leap_year(tsa).equals(pa.array(ts.dt.is_leap_year)) |
| 2661 | assert pc.month(tsa).equals(pa.array(month)) |
| 2662 | assert pc.day(tsa).equals(pa.array(day)) |
| 2663 | assert pc.day_of_week(tsa).equals(pa.array(dayofweek)) |
| 2664 | assert pc.day_of_year(tsa).equals(pa.array(dayofyear)) |
| 2665 | assert pc.iso_year(tsa).equals(pa.array(iso_year)) |
| 2666 | assert pc.iso_week(tsa).equals(pa.array(iso_week)) |
| 2667 | assert pc.iso_calendar(tsa).equals(iso_calendar) |
| 2668 | assert pc.quarter(tsa).equals(pa.array(quarter)) |
| 2669 | assert pc.hour(tsa).equals(pa.array(hour)) |
| 2670 | assert pc.minute(tsa).equals(pa.array(minute)) |
| 2671 | assert pc.second(tsa).equals(pa.array(second)) |
no test coverage detected