Skip to content

BUG: Pyarrow timestamp support for map() function #61236

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -844,6 +844,7 @@ Other
- Bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
- Bug in :meth:`Series.dt` methods in :class:`ArrowDtype` that were returning incorrect values. (:issue:`57355`)
- Bug in :meth:`Series.isin` raising ``TypeError`` when series is large (>10**6) and ``values`` contains NA (:issue:`60678`)
- Bug in :meth:`Series.map` where mapping with a ``dict`` failed to match keys when the Series used ``timestamp[ns][pyarrow]`` dtype. (:issue:`61231`)
- Bug in :meth:`Series.mode` where an exception was raised when taking the mode with nullable types with no null values in the series. (:issue:`58926`)
- Bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
lib,
)
from pandas._libs.missing import NA
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._typing import (
AnyArrayLike,
ArrayLike,
Expand Down Expand Up @@ -59,6 +60,7 @@
)
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.dtypes import (
ArrowDtype,
BaseMaskedDtype,
CategoricalDtype,
ExtensionDtype,
Expand Down Expand Up @@ -1691,6 +1693,14 @@ def map_array(
if na_action == "ignore":
mapper = mapper[mapper.index.notna()]

if isinstance(arr.dtype, ArrowDtype) and arr.dtype.name.startswith("timestamp"):
try:
# Convert elements to pandas.Timestamp (or datetime64[ns])
arr = arr.astype("datetime64[ns]")
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong place to fix this. This should be fixed in ArrowExtensionArray.map

# fallback: safe, slow path
arr = np.array([Timestamp(x.as_py()) for x in arr])

# Since values were input this means we came from either
# a dict or a series and mapper should be an index
indexer = mapper.index.get_indexer(arr)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,14 @@ def test_map_kwargs():
result = Series([2, 4, 5]).map(lambda x, y: x + y, y=2)
expected = Series([4, 6, 7])
tm.assert_series_equal(result, expected)


def test_map_arrow_timestamp_dict():
# GH 61231
pytest.importorskip("pyarrow", minversion="10.0.1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pytest.importorskip("pyarrow", minversion="10.0.1")
pytest.importorskip("pyarrow")


ser = Series(date_range("2023-01-01", periods=3)).astype("timestamp[ns][pyarrow]")
mapper = {ts: i for i, ts in enumerate(ser)}
result = ser.map(mapper)
expected = Series([0, 1, 2], dtype="int64")
tm.assert_series_equal(result, expected)
Loading