Skip to content

Commit 3caf55f

Browse files
authored
BUG: map(na_action=ignore) not respected for Arrow & masked types (#57388)
1 parent 560ec0c commit 3caf55f

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Fixed regressions
2424
- Fixed regression in :meth:`CategoricalIndex.difference` raising ``KeyError`` when other contains null values other than NaN (:issue:`57318`)
2525
- Fixed regression in :meth:`DataFrame.groupby` raising ``ValueError`` when grouping by a :class:`Series` in some cases (:issue:`57276`)
2626
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
27+
- Fixed regression in :meth:`DataFrame.map` with ``na_action="ignore"`` not being respected for NumPy nullable and :class:`ArrowDtypes` (:issue:`57316`)
2728
- Fixed regression in :meth:`DataFrame.merge` raising ``ValueError`` for certain types of 3rd-party extension arrays (:issue:`57316`)
2829
- Fixed regression in :meth:`DataFrame.shift` raising ``AssertionError`` for ``axis=1`` and empty :class:`DataFrame` (:issue:`57301`)
2930
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)

pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ def to_numpy(
14301430

14311431
def map(self, mapper, na_action=None):
14321432
if is_numeric_dtype(self.dtype):
1433-
return map_array(self.to_numpy(), mapper, na_action=None)
1433+
return map_array(self.to_numpy(), mapper, na_action=na_action)
14341434
else:
14351435
return super().map(mapper, na_action)
14361436

pandas/core/arrays/masked.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ def max(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
13351335
return self._wrap_reduction_result("max", result, skipna=skipna, axis=axis)
13361336

13371337
def map(self, mapper, na_action=None):
1338-
return map_array(self.to_numpy(), mapper, na_action=None)
1338+
return map_array(self.to_numpy(), mapper, na_action=na_action)
13391339

13401340
@overload
13411341
def any(

pandas/tests/extension/test_arrow.py

+7
Original file line numberDiff line numberDiff line change
@@ -3497,3 +3497,10 @@ def test_to_numpy_timestamp_to_int():
34973497
result = ser.to_numpy(dtype=np.int64)
34983498
expected = np.array([1577853000000000000])
34993499
tm.assert_numpy_array_equal(result, expected)
3500+
3501+
3502+
def test_map_numeric_na_action():
3503+
ser = pd.Series([32, 40, None], dtype="int64[pyarrow]")
3504+
result = ser.map(lambda x: 42, na_action="ignore")
3505+
expected = pd.Series([42.0, 42.0, np.nan], dtype="float64")
3506+
tm.assert_series_equal(result, expected)

pandas/tests/extension/test_masked.py

+9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ def test_map(self, data_missing, na_action):
179179
expected = data_missing.to_numpy()
180180
tm.assert_numpy_array_equal(result, expected)
181181

182+
def test_map_na_action_ignore(self, data_missing_for_sorting):
183+
zero = data_missing_for_sorting[2]
184+
result = data_missing_for_sorting.map(lambda x: zero, na_action="ignore")
185+
if data_missing_for_sorting.dtype.kind == "b":
186+
expected = np.array([False, pd.NA, False], dtype=object)
187+
else:
188+
expected = np.array([zero, np.nan, zero])
189+
tm.assert_numpy_array_equal(result, expected)
190+
182191
def _get_expected_exception(self, op_name, obj, other):
183192
try:
184193
dtype = tm.get_dtype(obj)

0 commit comments

Comments
 (0)