Skip to content

BUG: Fix .dt.microsecond accessor for pyarrow-backed Series #59183

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

Merged
merged 3 commits into from
Jul 8, 2024
Merged
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` producing incorrect results for pyarrow backed :class:`Series`. (: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
5 changes: 4 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,10 @@ def _dt_days_in_month(self) -> Self:

@property
def _dt_microsecond(self) -> Self:
return type(self)(pc.microsecond(self._pa_array))
# GH 59154
us = pc.microsecond(self._pa_array)
ms_to_us = pc.multiply(pc.millisecond(self._pa_array), 1000)
return type(self)(pc.add(us, ms_to_us))

@property
def _dt_minute(self) -> Self:
Expand Down
28 changes: 25 additions & 3 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2437,13 +2437,13 @@ def test_unsupported_dt(data):
["hour", 3],
["minute", 4],
["is_leap_year", False],
["microsecond", 5],
["microsecond", 2000],
["month", 1],
["nanosecond", 6],
["quarter", 1],
["second", 7],
["date", date(2023, 1, 2)],
["time", time(3, 4, 7, 5)],
["time", time(3, 4, 7, 2000)],
],
)
def test_dt_properties(prop, expected):
Expand All @@ -2456,7 +2456,7 @@ def test_dt_properties(prop, expected):
hour=3,
minute=4,
second=7,
microsecond=5,
microsecond=2000,
nanosecond=6,
),
None,
Expand All @@ -2473,6 +2473,28 @@ def test_dt_properties(prop, expected):
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("microsecond", [2000, 5, 0])
def test_dt_microsecond(microsecond):
# GH 59183
ser = pd.Series(
[
pd.Timestamp(
year=2024,
month=7,
day=7,
second=5,
microsecond=microsecond,
nanosecond=6,
),
None,
],
dtype=ArrowDtype(pa.timestamp("ns")),
)
result = ser.dt.microsecond
expected = pd.Series([microsecond, None], dtype="int64[pyarrow]")
tm.assert_series_equal(result, expected)


def test_dt_is_month_start_end():
ser = pd.Series(
[
Expand Down
Loading