Skip to content

Commit 0950b1c

Browse files
authored
BUG/API: DTI/TDI/PI.map na_action="ignore" (#51936)
* REF: simplify .map method for datetime-likes * sort issue numbers * fix comment
1 parent 7b93b06 commit 0950b1c

File tree

6 files changed

+12
-25
lines changed

6 files changed

+12
-25
lines changed

doc/source/whatsnew/v2.1.0.rst

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

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

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ def _map_values(self, mapper, na_action=None, convert: bool = True):
895895
return arr.map(mapper, na_action=na_action)
896896

897897
# Argument 1 to "map_array" has incompatible type
898-
# "Union[IndexOpsMixin, ExtensionArray, ndarray[Any, Any]]";
899-
# expected "Union[ExtensionArray, ndarray[Any, Any]]"
898+
# "Union[IndexOpsMixin, ndarray[Any, Any]]";
899+
# expected "Union[ExtensionArray, ndarray[Any, Any]]
900900
return algorithms.map_array(
901901
arr, mapper, na_action=na_action, convert=convert # type: ignore[arg-type]
902902
)

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)