Skip to content

Commit 3dca4f0

Browse files
Backport PR pandas-dev#57388 on branch 2.2.x (BUG: map(na_action=ignore) not respected for Arrow & masked types) (pandas-dev#57413)
Backport PR pandas-dev#57388: BUG: map(na_action=ignore) not respected for Arrow & masked types Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 09debec commit 3dca4f0

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
@@ -1414,7 +1414,7 @@ def to_numpy(
14141414

14151415
def map(self, mapper, na_action=None):
14161416
if is_numeric_dtype(self.dtype):
1417-
return map_array(self.to_numpy(), mapper, na_action=None)
1417+
return map_array(self.to_numpy(), mapper, na_action=na_action)
14181418
else:
14191419
return super().map(mapper, na_action)
14201420

pandas/core/arrays/masked.py

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

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

13381338
def any(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
13391339
"""

pandas/tests/extension/test_arrow.py

+7
Original file line numberDiff line numberDiff line change
@@ -3379,3 +3379,10 @@ def test_to_numpy_timestamp_to_int():
33793379
result = ser.to_numpy(dtype=np.int64)
33803380
expected = np.array([1577853000000000000])
33813381
tm.assert_numpy_array_equal(result, expected)
3382+
3383+
3384+
def test_map_numeric_na_action():
3385+
ser = pd.Series([32, 40, None], dtype="int64[pyarrow]")
3386+
result = ser.map(lambda x: 42, na_action="ignore")
3387+
expected = pd.Series([42.0, 42.0, np.nan], dtype="float64")
3388+
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)