Skip to content

Commit 6df8062

Browse files
author
Rohan Jain
committed
Series.dt.microsecond fix for pyarrow types
1 parent 8bca186 commit 6df8062

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ Datetimelike
500500
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` does not raise on Custom business days frequencies bigger then "1C" (:issue:`58664`)
501501
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
502502
- Bug in :meth:`DatetimeIndex.union` when ``unit`` was non-nanosecond (:issue:`59036`)
503+
- Bug in :meth:`Series.dt.microsecond` with:class:`ArrowDtype` returning incorrect values. (:issue:`59154`)
503504
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
504505

505506
Timedelta

pandas/core/arrays/arrow/array.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,14 @@ def _dt_days_in_month(self) -> Self:
27942794

27952795
@property
27962796
def _dt_microsecond(self) -> Self:
2797-
return type(self)(pc.microsecond(self._pa_array))
2797+
us = pc.microsecond(self._pa_array)
2798+
ms_us = pc.multiply(
2799+
pc.millisecond(self._pa_array), pa.scalar(1000, type=us.type)
2800+
)
2801+
s_us = pc.multiply(
2802+
pc.second(self._pa_array), pa.scalar(1_000_000, type=us.type)
2803+
)
2804+
return type(self)(pc.add(pc.add(ms_us, s_us), us))
27982805

27992806
@property
28002807
def _dt_minute(self) -> Self:

pandas/tests/extension/test_arrow.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3536,3 +3536,13 @@ def test_map_numeric_na_action():
35363536
result = ser.map(lambda x: 42, na_action="ignore")
35373537
expected = pd.Series([42.0, 42.0, np.nan], dtype="float64")
35383538
tm.assert_series_equal(result, expected)
3539+
3540+
3541+
def test_microsecond():
3542+
series = pd.Series(
3543+
[946684800000000000, 946684800015000000, 946684800030000000],
3544+
dtype=ArrowDtype(pa.timestamp("ns")),
3545+
)
3546+
result = series.dt.microsecond
3547+
expected = pd.Series([0, 15000, 30000], dtype="int64[pyarrow]")
3548+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)