Skip to content

Commit f8153f3

Browse files
committed
Fix pandas-dev#60766:.map,.apply would convert element type for extension array.
The Int32Dtype type allows representing integers with support for null values (pd.NA). However, when using .map(f) or .apply(f), the elements passed to f are converted to float64, and pd.NA is transformed into np.nan. This happens because .map() and .apply() internally use numpy, which automatically converts the data to float64, even when the original type is Int32Dtype. The fix (just remove the method to_numpy()) ensures that when using .map() or .apply(), the elements in the series retain their original type (Int32, Float64, boolean, etc.), preventing unnecessary conversions to float64 and ensuring that pd.NA remains correctly handled.
1 parent b64f438 commit f8153f3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/core/arrays/masked.py

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

13271327
def map(self, mapper, na_action: Literal["ignore"] | None = None):
1328-
return map_array(self.to_numpy(), mapper, na_action=na_action)
1328+
return map_array(self, mapper, na_action=na_action)
13291329

13301330
@overload
13311331
def any(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pandas as pd
2+
3+
def test_basemaskedarray_map():
4+
for dtype, data, expected_data in [
5+
("Int32", [1, 2, None, 4], [2, 3, pd.NA, 5]),
6+
7+
]:
8+
s = pd.Series(data, dtype=dtype)
9+
10+
def transform(x):
11+
if x is None:
12+
return x
13+
return x + 1
14+
15+
result = s.map(transform)
16+
expected = pd.Series(expected_data, dtype=result.dtype)
17+
18+
assert result.tolist() == expected.tolist()

0 commit comments

Comments
 (0)