Skip to content

Commit 8e06eb7

Browse files
committed
REF: simplify .map method for datetime-likes
1 parent fb282b6 commit 8e06eb7

File tree

6 files changed

+10
-26
lines changed

6 files changed

+10
-26
lines changed

doc/source/whatsnew/v2.1.0.rst

+5
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,17 @@ Categorical
140140
Datetimelike
141141
^^^^^^^^^^^^
142142
- Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`)
143+
- :meth:`arrays.DatetimeArray.map` can now take a ``na_action`` argument. :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
143144
- Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
145+
-
144146

145147
Timedelta
146148
^^^^^^^^^
147149
- Bug in :meth:`Timedelta.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`51494`)
148150
- Bug in :class:`TimedeltaIndex` division or multiplication leading to ``.freq`` of "0 Days" instead of ``None`` (:issue:`51575`)
151+
- :meth:`arrays.TimedeltaArray.map` can now take a ``na_action`` argument. :meth:`TimedeltaIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
149152
- Bug in :meth:`arrays.TimedeltaArray.map` and :meth:`TimedeltaIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
153+
-
150154

151155
Timezones
152156
^^^^^^^^^
@@ -197,6 +201,7 @@ Period
197201
^^^^^^
198202
- Bug in :class:`PeriodDtype` constructor failing to raise ``TypeError`` when no argument is passed or when ``None`` is passed (:issue:`27388`)
199203
- Bug in :class:`PeriodDtype` constructor raising ``ValueError`` instead of ``TypeError`` when an invalid type is passed (:issue:`51790`)
204+
- :meth:`arrays.PeriodArray.map` can now take a ``na_action`` argument. :meth:`PeriodIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
200205
- Bug in :meth:`arrays.PeriodArray.map` and :meth:`PeriodIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
201206
-
202207

pandas/core/arrays/datetimelike.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,9 @@ def _unbox(self, other) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarra
747747

748748
@ravel_compat
749749
def map(self, mapper, na_action=None):
750-
if na_action is not None:
751-
raise NotImplementedError
752-
753750
from pandas import Index
754751

755-
result = map_array(self, mapper)
752+
result = map_array(self, mapper, na_action=na_action)
756753
result = Index(result)
757754

758755
if isinstance(result, ABCMultiIndex):

pandas/core/base.py

-3
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,6 @@ def _map_values(self, mapper, na_action=None, convert: bool = True):
894894
if isinstance(arr, ExtensionArray):
895895
return arr.map(mapper, na_action=na_action)
896896

897-
# Argument 1 to "map_array" has incompatible type
898-
# "Union[IndexOpsMixin, ExtensionArray, ndarray[Any, Any]]";
899-
# expected "Union[ExtensionArray, ndarray[Any, Any]]"
900897
return algorithms.map_array(
901898
arr, mapper, na_action=na_action, convert=convert # type: ignore[arg-type]
902899
)

pandas/tests/apply/test_invalid_arg.py

-7
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ def test_map_categorical_na_action():
8383
s.map(lambda x: x, na_action="ignore")
8484

8585

86-
def test_map_datetimetz_na_action():
87-
values = date_range("2011-01-01", "2011-01-02", freq="H").tz_localize("Asia/Tokyo")
88-
s = Series(values, name="XX")
89-
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
90-
s.map(lambda x: x, na_action="ignore")
91-
92-
9386
@pytest.mark.parametrize("method", ["apply", "agg", "transform"])
9487
@pytest.mark.parametrize("func", [{"A": {"B": "sum"}}, {"A": {"B": ["sum"]}}])
9588
def test_nested_renamer(frame_or_series, method, func):

pandas/tests/extension/test_datetime.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,8 @@ def test_combine_add(self, data_repeated):
118118

119119
@pytest.mark.parametrize("na_action", [None, "ignore"])
120120
def test_map(self, data, na_action):
121-
if na_action is not None:
122-
with pytest.raises(NotImplementedError, match=""):
123-
data.map(lambda x: x, na_action=na_action)
124-
else:
125-
result = data.map(lambda x: x, na_action=na_action)
126-
self.assert_extension_array_equal(result, data)
121+
result = data.map(lambda x: x, na_action=na_action)
122+
self.assert_extension_array_equal(result, data)
127123

128124

129125
class TestInterface(BaseDatetimeTests, base.BaseInterfaceTests):

pandas/tests/extension/test_period.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,8 @@ def test_diff(self, data, periods):
107107

108108
@pytest.mark.parametrize("na_action", [None, "ignore"])
109109
def test_map(self, data, na_action):
110-
if na_action is not None:
111-
with pytest.raises(NotImplementedError, match=""):
112-
data.map(lambda x: x, na_action=na_action)
113-
else:
114-
result = data.map(lambda x: x, na_action=na_action)
115-
self.assert_extension_array_equal(result, data)
110+
result = data.map(lambda x: x, na_action=na_action)
111+
self.assert_extension_array_equal(result, data)
116112

117113

118114
class TestInterface(BasePeriodTests, base.BaseInterfaceTests):

0 commit comments

Comments
 (0)