Skip to content

Commit aaaac86

Browse files
h-vetinarijreback
authored andcommitted
API: fix corner case of lib.infer_dtype (#23422)
1 parent 0fe97bc commit aaaac86

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

pandas/_libs/lib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ from tslibs.conversion cimport convert_to_tsobject
5757
from tslibs.timedeltas cimport convert_to_timedelta64
5858
from tslibs.timezones cimport get_timezone, tz_compare
5959

60-
from missing cimport (checknull,
60+
from missing cimport (checknull, isnaobj,
6161
is_null_datetime64, is_null_timedelta64, is_null_period)
6262

6363

@@ -1181,6 +1181,9 @@ def infer_dtype(value: object, skipna: bool=False) -> str:
11811181
values = construct_1d_object_array_from_listlike(value)
11821182

11831183
values = getattr(values, 'values', values)
1184+
if skipna:
1185+
values = values[~isnaobj(values)]
1186+
11841187
val = _try_infer_map(values)
11851188
if val is not None:
11861189
return val

pandas/_libs/missing.pxd

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# -*- coding: utf-8 -*-
22

3+
from numpy cimport ndarray, uint8_t
4+
35
cpdef bint checknull(object val)
46
cpdef bint checknull_old(object val)
7+
cpdef ndarray[uint8_t] isnaobj(ndarray arr)
58

69
cdef bint is_null_datetime64(v)
710
cdef bint is_null_timedelta64(v)

pandas/_libs/missing.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ cdef inline bint _check_none_nan_inf_neginf(object val):
124124

125125
@cython.wraparound(False)
126126
@cython.boundscheck(False)
127-
def isnaobj(ndarray arr):
127+
cpdef ndarray[uint8_t] isnaobj(ndarray arr):
128128
"""
129129
Return boolean mask denoting which elements of a 1-D array are na-like,
130130
according to the criteria defined in `_check_all_nulls`:

pandas/tests/dtypes/test_inference.py

+16
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,22 @@ def test_unicode(self):
591591
expected = 'unicode' if PY2 else 'string'
592592
assert result == expected
593593

594+
@pytest.mark.parametrize('dtype, missing, skipna, expected', [
595+
(float, np.nan, False, 'floating'),
596+
(float, np.nan, True, 'floating'),
597+
(object, np.nan, False, 'floating'),
598+
(object, np.nan, True, 'empty'),
599+
(object, None, False, 'mixed'),
600+
(object, None, True, 'empty')
601+
])
602+
@pytest.mark.parametrize('box', [pd.Series, np.array])
603+
def test_object_empty(self, box, missing, dtype, skipna, expected):
604+
# GH 23421
605+
arr = box([missing, missing], dtype=dtype)
606+
607+
result = lib.infer_dtype(arr, skipna=skipna)
608+
assert result == expected
609+
594610
def test_datetime(self):
595611

596612
dates = [datetime(2012, 1, x) for x in range(1, 20)]

0 commit comments

Comments
 (0)