(timestamps, timezone=None)
| 2466 | |
| 2467 | |
| 2468 | def _check_datetime_components(timestamps, timezone=None): |
| 2469 | from pyarrow.vendored.version import Version |
| 2470 | |
| 2471 | ts = pd.to_datetime(timestamps).tz_localize( |
| 2472 | "UTC").tz_convert(timezone).to_series() |
| 2473 | tsa = pa.array(ts, pa.timestamp("ns", tz=timezone)) |
| 2474 | |
| 2475 | subseconds = ((ts.dt.microsecond * 10 ** 3 + |
| 2476 | ts.dt.nanosecond) * 10 ** -9).round(9) |
| 2477 | iso_calendar_fields = [ |
| 2478 | pa.field('iso_year', pa.int64()), |
| 2479 | pa.field('iso_week', pa.int64()), |
| 2480 | pa.field('iso_day_of_week', pa.int64()) |
| 2481 | ] |
| 2482 | |
| 2483 | if Version(pd.__version__) < Version("1.1.0"): |
| 2484 | # https://github.com/pandas-dev/pandas/issues/33206 |
| 2485 | iso_year = ts.map(lambda x: x.isocalendar()[0]).astype("int64") |
| 2486 | iso_week = ts.map(lambda x: x.isocalendar()[1]).astype("int64") |
| 2487 | iso_day = ts.map(lambda x: x.isocalendar()[2]).astype("int64") |
| 2488 | else: |
| 2489 | # Casting is required because pandas isocalendar returns int32 |
| 2490 | # while arrow isocalendar returns int64. |
| 2491 | iso_year = ts.dt.isocalendar()["year"].astype("int64") |
| 2492 | iso_week = ts.dt.isocalendar()["week"].astype("int64") |
| 2493 | iso_day = ts.dt.isocalendar()["day"].astype("int64") |
| 2494 | |
| 2495 | iso_calendar = pa.StructArray.from_arrays( |
| 2496 | [iso_year, iso_week, iso_day], |
| 2497 | fields=iso_calendar_fields) |
| 2498 | |
| 2499 | # Casting is required because pandas with 2.0.0 various numeric |
| 2500 | # date/time attributes have dtype int32 (previously int64) |
| 2501 | year = ts.dt.year.astype("int64") |
| 2502 | month = ts.dt.month.astype("int64") |
| 2503 | day = ts.dt.day.astype("int64") |
| 2504 | dayofweek = ts.dt.dayofweek.astype("int64") |
| 2505 | dayofyear = ts.dt.dayofyear.astype("int64") |
| 2506 | quarter = ts.dt.quarter.astype("int64") |
| 2507 | hour = ts.dt.hour.astype("int64") |
| 2508 | minute = ts.dt.minute.astype("int64") |
| 2509 | second = ts.dt.second.values.astype("int64") |
| 2510 | microsecond = ts.dt.microsecond.astype("int64") |
| 2511 | nanosecond = ts.dt.nanosecond.astype("int64") |
| 2512 | |
| 2513 | assert pc.year(tsa).equals(pa.array(year)) |
| 2514 | assert pc.is_leap_year(tsa).equals(pa.array(ts.dt.is_leap_year)) |
| 2515 | assert pc.month(tsa).equals(pa.array(month)) |
| 2516 | assert pc.day(tsa).equals(pa.array(day)) |
| 2517 | assert pc.day_of_week(tsa).equals(pa.array(dayofweek)) |
| 2518 | assert pc.day_of_year(tsa).equals(pa.array(dayofyear)) |
| 2519 | assert pc.iso_year(tsa).equals(pa.array(iso_year)) |
| 2520 | assert pc.iso_week(tsa).equals(pa.array(iso_week)) |
| 2521 | assert pc.iso_calendar(tsa).equals(iso_calendar) |
| 2522 | assert pc.quarter(tsa).equals(pa.array(quarter)) |
| 2523 | assert pc.hour(tsa).equals(pa.array(hour)) |
| 2524 | assert pc.minute(tsa).equals(pa.array(minute)) |
| 2525 | assert pc.second(tsa).equals(pa.array(second)) |
no test coverage detected