Skip to content

Commit 378620e

Browse files
Backport PR #71865 on branch 3.1.x (REGR: ensure map() with datetime-like dtype gives writeable result) (#71868)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent ceca8cb commit 378620e

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎doc/source/whatsnew/v3.1.1.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression in :meth:`DataFrame.astype`, :meth:`Series.astype` and :meth:`DataFrame.convert_dtypes` with ``dtype_backend="pyarrow"`` overwriting missing values of the original datetime data with the Unix epoch when converting to a pyarrow timestamp type (:issue:`71743`)
17-
17+
- Fixed regression in :meth:`Series.map` with datetime-like values returning a read-only Series, causing subsequent assignment to fail (:issue:`71567`)
1818

1919
.. ---------------------------------------------------------------------------
2020
.. _whatsnew_311.bugs:

‎pandas/core/arrays/datetimelike.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,12 @@ def map(self, mapper, na_action: Literal["ignore"] | None = None):
758758
from pandas import Index
759759

760760
result = map_array(self, mapper, na_action=na_action)
761-
result = Index(result)
761+
result = Index(result, copy=False)
762762

763763
if isinstance(result, ABCMultiIndex):
764764
return result.to_numpy()
765765
else:
766-
return result.array
766+
return result._data
767767

768768
def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
769769
"""

‎pandas/tests/series/methods/test_map.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,42 @@ def test_map_box_period():
483483
tm.assert_series_equal(res, exp)
484484

485485

486+
@pytest.mark.parametrize(
487+
"data,mapper",
488+
[
489+
(
490+
pd.date_range("2026-10-24", periods=3, freq="h"),
491+
lambda ts: ts.replace(year=2067),
492+
),
493+
(
494+
pd.date_range("2026-10-24", periods=3, freq="h", tz="UTC"),
495+
lambda ts: ts.replace(year=2067),
496+
),
497+
(
498+
pd.timedelta_range("1 day", periods=3),
499+
lambda td: td * 2,
500+
),
501+
(
502+
pd.period_range("2026-10", periods=3, freq="M"),
503+
lambda period: period + 1,
504+
),
505+
],
506+
ids=["datetime64", "datetime64_tz", "timedelta64", "period"],
507+
)
508+
def test_map_datetimelike_result_writable(data, mapper):
509+
# https://github.com/pandas-dev/pandas/issues/71567
510+
ser = pd.Series(data)
511+
result = ser.map(mapper)
512+
assert result._values._readonly is False
513+
514+
expected = pd.Series([mapper(x) for x in data])
515+
tm.assert_series_equal(result, expected)
516+
517+
# ensure the result is still writable through assignment
518+
result.iloc[0] = result.iloc[1]
519+
assert result.iloc[0] == result.iloc[1]
520+
521+
486522
def test_map_categorical(na_action, using_infer_string):
487523
values = pd.Categorical(list("ABBABCD"), categories=list("DCBA"), ordered=True)
488524
s = pd.Series(values, name="XX", index=list("abcdefg"))

0 commit comments

Comments
 (0)