Skip to content

Commit

Permalink
Backport PR pandas-dev#57388: BUG: map(na_action=ignore) not respecte…
Browse files Browse the repository at this point in the history
…d for Arrow & masked types
  • Loading branch information
mroeschke authored and meeseeksmachine committed Feb 14, 2024
1 parent 09debec commit 96d5744
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fixed regressions
- Fixed regression in :meth:`CategoricalIndex.difference` raising ``KeyError`` when other contains null values other than NaN (:issue:`57318`)
- Fixed regression in :meth:`DataFrame.groupby` raising ``ValueError`` when grouping by a :class:`Series` in some cases (:issue:`57276`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
- Fixed regression in :meth:`DataFrame.map` with ``na_action="ignore"`` not being respected for NumPy nullable and :class:`ArrowDtypes` (:issue:`57316`)
- Fixed regression in :meth:`DataFrame.merge` raising ``ValueError`` for certain types of 3rd-party extension arrays (:issue:`57316`)
- Fixed regression in :meth:`DataFrame.shift` raising ``AssertionError`` for ``axis=1`` and empty :class:`DataFrame` (:issue:`57301`)
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ def to_numpy(

def map(self, mapper, na_action=None):
if is_numeric_dtype(self.dtype):
return map_array(self.to_numpy(), mapper, na_action=None)
return map_array(self.to_numpy(), mapper, na_action=na_action)
else:
return super().map(mapper, na_action)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ def max(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
return self._wrap_reduction_result("max", result, skipna=skipna, axis=axis)

def map(self, mapper, na_action=None):
return map_array(self.to_numpy(), mapper, na_action=None)
return map_array(self.to_numpy(), mapper, na_action=na_action)

def any(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3379,3 +3379,10 @@ def test_to_numpy_timestamp_to_int():
result = ser.to_numpy(dtype=np.int64)
expected = np.array([1577853000000000000])
tm.assert_numpy_array_equal(result, expected)


def test_map_numeric_na_action():
ser = pd.Series([32, 40, None], dtype="int64[pyarrow]")
result = ser.map(lambda x: 42, na_action="ignore")
expected = pd.Series([42.0, 42.0, np.nan], dtype="float64")
tm.assert_series_equal(result, expected)
9 changes: 9 additions & 0 deletions pandas/tests/extension/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ def test_map(self, data_missing, na_action):
expected = data_missing.to_numpy()
tm.assert_numpy_array_equal(result, expected)

def test_map_na_action_ignore(self, data_missing_for_sorting):
zero = data_missing_for_sorting[2]
result = data_missing_for_sorting.map(lambda x: zero, na_action="ignore")
if data_missing_for_sorting.dtype.kind == "b":
expected = np.array([False, pd.NA, False], dtype=object)
else:
expected = np.array([zero, np.nan, zero])
tm.assert_numpy_array_equal(result, expected)

def _get_expected_exception(self, op_name, obj, other):
try:
dtype = tm.get_dtype(obj)
Expand Down

0 comments on commit 96d5744

Please sign in to comment.