Skip to content

Commit 6c8ae4d

Browse files
committed
added dtype check for series apply and map
1 parent e557039 commit 6c8ae4d

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

pandas/core/algorithms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import numpy as np
1919

20+
import pandas as pd
2021
from pandas._libs import (
2122
algos,
2223
hashtable as htable,
@@ -1648,9 +1649,15 @@ def map_array(
16481649
a MultiIndex will be returned.
16491650
"""
16501651
if na_action not in (None, "ignore"):
1651-
msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
1652+
msg = f"na_acti(on must either be 'ignore' or None, {na_action} was passed"
16521653
raise ValueError(msg)
16531654

1655+
check = pd.isna(arr)
1656+
1657+
def apply_map(x):
1658+
if na_action == "ignore" and pd.isna(x):
1659+
return x
1660+
16541661
# we can fastpath dict/Series to an efficient map
16551662
# as we know that we are not going to have to yield
16561663
# python types

pandas/core/apply.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515

1616
import numpy as np
17+
from pandas.core.dtypes.missing import isna
1718

1819
from pandas._libs.internals import BlockValuesRefs
1920
from pandas._typing import (
@@ -1389,7 +1390,8 @@ def __init__(
13891390

13901391
def apply(self) -> DataFrame | Series:
13911392
obj = self.obj
1392-
1393+
1394+
13931395
if len(obj) == 0:
13941396
return self.apply_empty_result()
13951397

pandas/core/series.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,12 +4404,22 @@ def map(
44044404
3 I am a rabbit
44054405
dtype: object
44064406
"""
4407-
if callable(arg):
4408-
arg = functools.partial(arg, **kwargs)
4409-
new_values = self._map_values(arg, na_action=na_action)
4410-
return self._constructor(new_values, index=self.index, copy=False).__finalize__(
4411-
self, method="map"
4412-
)
4407+
#Check if the dtype is an integer
4408+
if pd.api.types.is_integer_dtype(self) and pd.api.types.is_nullable(self.dtype):
4409+
#if dtype is nullable int type, ensure NaN values replaced with pd.NA
4410+
def map_check(val):
4411+
if val is None:
4412+
return pd.NA
4413+
return val
4414+
arg = map_check(arg)
4415+
4416+
else:
4417+
if callable(arg):
4418+
arg = functools.partial(arg, **kwargs)
4419+
new_values = self._map_values(arg, na_action=na_action)
4420+
return self._constructor(new_values, index=self.index, copy=False).__finalize__(
4421+
self, method="map"
4422+
)
44134423

44144424
def _gotitem(self, key, ndim, subset=None) -> Self:
44154425
"""
@@ -4609,6 +4619,16 @@ def apply(
46094619
Helsinki 2.484907
46104620
dtype: float64
46114621
"""
4622+
# check if dtype is nullable integer
4623+
if pd.api.types.is_integer_dtype(self) and pd.api.types.is_nullable(self.dtype):
4624+
# def functon to handle NaN as pd.NA
4625+
def apply_check(val):
4626+
if val is None:
4627+
return pd.NA
4628+
return val
4629+
func = functools.partial(apply_check,func)
4630+
4631+
#proceed with usual apply method
46124632
return SeriesApply(
46134633
self,
46144634
func,

0 commit comments

Comments
 (0)