Skip to content

Commit 780d48c

Browse files
committed
additional fixes plus tests
1 parent e605f2f commit 780d48c

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

pandas/core/algorithms.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,12 +1651,7 @@ def map_array(
16511651
if na_action not in (None, "ignore"):
16521652
msg = f"na_acti(on must either be 'ignore' or None, {na_action} was passed"
16531653
raise ValueError(msg)
1654-
1655-
check = pd.isna(arr)
16561654

1657-
def apply_map(x):
1658-
if na_action == "ignore" and pd.isna(x):
1659-
return x
16601655

16611656
# we can fastpath dict/Series to an efficient map
16621657
# as we know that we are not going to have to yield
@@ -1701,8 +1696,18 @@ def apply_map(x):
17011696
return arr.copy()
17021697

17031698
# we must convert to python types
1704-
values = arr.astype(object, copy=False)
1705-
if na_action is None:
1706-
return lib.map_infer(values, mapper)
1707-
else:
1708-
return lib.map_infer_mask(values, mapper, mask=isna(values).view(np.uint8))
1699+
#values = arr.astype(object, copy=False)
1700+
1701+
if is_integer_dtype(arr) and is_nullable_dtype(arr.dtype):
1702+
def mapper_check(x):
1703+
if x is None:
1704+
return pd.NA
1705+
else:
1706+
mapper(x)
1707+
values = arr.copy()
1708+
1709+
if na_action is None:
1710+
#return lib.map_infer(values, mapper)
1711+
return np.array([mapper_check(x) for x in values], dtype = arr.dtype)
1712+
else:
1713+
return lib.map_infer_mask(values, mapper, mask=isna(values).view(np.uint8))

pandas/core/apply.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,14 +1457,18 @@ def apply_standard(self) -> DataFrame | Series:
14571457
return func(obj, *self.args, **self.kwargs)
14581458
elif not self.by_row:
14591459
return func(obj, *self.args, **self.kwargs)
1460-
1461-
if self.args or self.kwargs:
1462-
# _map_values does not support args/kwargs
1463-
def curried(x):
1464-
return func(x, *self.args, **self.kwargs)
1460+
1461+
#Check if type is integer and nullable, return pd.NA for None values and
1462+
#normal func for other values
1463+
if pd.api.types.is_integer_dtype(obj) and pd.api.types.is_nullable_dtype(obj.dtype):
1464+
def wrapped_func(x):
1465+
if x is None:
1466+
return pd.NA
1467+
return func(x,*self.args, **self.kwargs)
14651468

14661469
else:
14671470
curried = func
1471+
14681472
mapped = obj._map_values(mapper=curried)
14691473

14701474
if len(mapped) and isinstance(mapped[0], ABCSeries):

pandas/tests/apply/test_series_apply.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,4 @@ def test_series_apply_unpack_nested_data():
665665
result = ser.apply(lambda x: Series(x))
666666
expected = DataFrame({0: [1.0, 4.0], 1: [2.0, 5.0], 2: [3.0, 6.0], 3: [np.nan, 7]})
667667
tm.assert_frame_equal(result, expected)
668+

0 commit comments

Comments
 (0)