File tree Expand file tree Collapse file tree 3 files changed +37
-8
lines changed
Expand file tree Collapse file tree 3 files changed +37
-8
lines changed Original file line number Diff line number Diff line change 1717
1818import numpy as np
1919
20+ import pandas as pd
2021from 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
Original file line number Diff line number Diff line change 1414)
1515
1616import numpy as np
17+ from pandas .core .dtypes .missing import isna
1718
1819from pandas ._libs .internals import BlockValuesRefs
1920from 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
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments