Skip to content

Series.dt.microsecond fix for pyarrow types #59162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ Datetimelike
- 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`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
- Bug in :meth:`DatetimeIndex.union` when ``unit`` was non-nanosecond (:issue:`59036`)
- Bug in :meth:`Series.dt.microsecond` with:class:`ArrowDtype` returning incorrect values. (:issue:`59154`)
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)

Timedelta
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,14 @@ def _dt_days_in_month(self) -> Self:

@property
def _dt_microsecond(self) -> Self:
return type(self)(pc.microsecond(self._pa_array))
us = pc.microsecond(self._pa_array)
ms_us = pc.multiply(
pc.millisecond(self._pa_array), pa.scalar(1000, type=us.type)
)
s_us = pc.multiply(
pc.second(self._pa_array), pa.scalar(1_000_000, type=us.type)
)
return type(self)(pc.add(pc.add(ms_us, s_us), us))

@property
def _dt_minute(self) -> Self:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3536,3 +3536,13 @@ def test_map_numeric_na_action():
result = ser.map(lambda x: 42, na_action="ignore")
expected = pd.Series([42.0, 42.0, np.nan], dtype="float64")
tm.assert_series_equal(result, expected)


def test_microsecond():
series = pd.Series(
[946684800000000000, 946684800015000000, 946684800030000000],
dtype=ArrowDtype(pa.timestamp("ns")),
)
result = series.dt.microsecond
expected = pd.Series([0, 15000, 30000], dtype="int64[pyarrow]")
tm.assert_series_equal(result, expected)
Loading