@@ -971,6 +971,7 @@ cdef class Seen:
971
971
bint nat_ # seen nat
972
972
bint bool_ # seen_bool
973
973
bint null_ # seen_null
974
+ bint nan_ # seen_np.nan
974
975
bint uint_ # seen_uint (unsigned integer)
975
976
bint sint_ # seen_sint (signed integer)
976
977
bint float_ # seen_float
@@ -995,6 +996,7 @@ cdef class Seen:
995
996
self .nat_ = 0
996
997
self .bool_ = 0
997
998
self .null_ = 0
999
+ self .nan_ = 0
998
1000
self .uint_ = 0
999
1001
self .sint_ = 0
1000
1002
self .float_ = 0
@@ -1953,10 +1955,37 @@ def maybe_convert_numeric(ndarray[object] values, set na_values,
1953
1955
@ cython.wraparound (False )
1954
1956
def maybe_convert_objects (ndarray[object] objects , bint try_float = 0 ,
1955
1957
bint safe = 0 , bint convert_datetime = 0 ,
1956
- bint convert_timedelta = 0 ):
1958
+ bint convert_timedelta = 0 ,
1959
+ bint convert_to_nullable_integer = 0 ):
1957
1960
"""
1958
1961
Type inference function-- convert object array to proper dtype
1962
+
1963
+ Parameters
1964
+ ----------
1965
+ values : ndarray
1966
+ Array of object elements to convert.
1967
+ try_float : bool, default False
1968
+ If an array-like object contains only float or NaN values is
1969
+ encountered, whether to convert and return an array of float dtype.
1970
+ safe : bool, default False
1971
+ Whether to upcast numeric type (e.g. int cast to float). If set to
1972
+ True, no upcasting will be performed.
1973
+ convert_datetime : bool, default False
1974
+ If an array-like object contains only datetime values or NaT is
1975
+ encountered, whether to convert and return an array of M8[ns] dtype.
1976
+ convert_timedelta : bool, default False
1977
+ If an array-like object contains only timedelta values or NaT is
1978
+ encountered, whether to convert and return an array of m8[ns] dtype.
1979
+ convert_to_nullable_integer : bool, default False
1980
+ If an array-like object contains only interger values (and NaN) is
1981
+ encountered, whether to convert and return an IntegerArray.
1982
+
1983
+ Returns
1984
+ -------
1985
+ array : array of converted object values to more specific dtypes if
1986
+ pplicable
1959
1987
"""
1988
+
1960
1989
cdef:
1961
1990
Py_ssize_t i, n
1962
1991
ndarray[float64_t] floats
@@ -1977,6 +2006,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
1977
2006
ints = np.empty(n, dtype = ' i8' )
1978
2007
uints = np.empty(n, dtype = ' u8' )
1979
2008
bools = np.empty(n, dtype = np.uint8)
2009
+ mask = np.full(n, False )
1980
2010
1981
2011
if convert_datetime:
1982
2012
datetimes = np.empty(n, dtype = ' M8[ns]' )
@@ -1994,6 +2024,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
1994
2024
if val is None :
1995
2025
seen.null_ = 1
1996
2026
floats[i] = complexes[i] = fnan
2027
+ mask[i] = True
1997
2028
elif val is NaT:
1998
2029
seen.nat_ = 1
1999
2030
if convert_datetime:
@@ -2003,6 +2034,10 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
2003
2034
if not (convert_datetime or convert_timedelta):
2004
2035
seen.object_ = 1
2005
2036
break
2037
+ elif val is np.nan:
2038
+ seen.nan_ = 1
2039
+ mask[i] = True
2040
+ floats[i] = complexes[i] = val
2006
2041
elif util.is_bool_object(val):
2007
2042
seen.bool_ = 1
2008
2043
bools[i] = val
@@ -2084,11 +2119,19 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
2084
2119
2085
2120
if not seen.object_:
2086
2121
if not safe:
2087
- if seen.null_:
2122
+ if seen.null_ or seen.nan_ :
2088
2123
if seen.is_float_or_complex:
2089
2124
if seen.complex_:
2090
2125
return complexes
2091
- elif seen.float_ or seen.int_:
2126
+ elif seen.float_:
2127
+ return floats
2128
+ elif seen.int_:
2129
+ if convert_to_nullable_integer:
2130
+ from pandas.core.arrays import IntegerArray
2131
+ return IntegerArray(ints, mask)
2132
+ else :
2133
+ return floats
2134
+ elif seen.nan_:
2092
2135
return floats
2093
2136
else :
2094
2137
if not seen.bool_:
@@ -2127,7 +2170,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
2127
2170
if seen.complex_:
2128
2171
if not seen.int_:
2129
2172
return complexes
2130
- elif seen.float_:
2173
+ elif seen.float_ or seen.nan_ :
2131
2174
if not seen.int_:
2132
2175
return floats
2133
2176
else :
@@ -2151,7 +2194,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
2151
2194
if seen.complex_:
2152
2195
if not seen.int_:
2153
2196
return complexes
2154
- elif seen.float_:
2197
+ elif seen.float_ or seen.nan_ :
2155
2198
if not seen.int_:
2156
2199
return floats
2157
2200
elif seen.int_:
0 commit comments