Skip to content

Commit 1347658

Browse files
h-vetinaritm9k1
authored andcommitted
API: fix corner case of lib.infer_dtype (pandas-dev#23422)
1 parent d2857e0 commit 1347658

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
@@ -59,7 +59,7 @@ from tslibs.conversion cimport convert_to_tsobject
5959
from tslibs.timedeltas cimport convert_to_timedelta64
6060
from tslibs.timezones cimport get_timezone, tz_compare
6161

62-
from missing cimport (checknull,
62+
from missing cimport (checknull, isnaobj,
6363
is_null_datetime64, is_null_timedelta64, is_null_period)
6464

6565

@@ -1216,6 +1216,9 @@ def infer_dtype(value: object, skipna: bool=False) -> str:
12161216
values = construct_1d_object_array_from_listlike(value)
12171217

12181218
values = getattr(values, 'values', values)
1219+
if skipna:
1220+
values = values[~isnaobj(values)]
1221+
12191222
val = _try_infer_map(values)
12201223
if val is not None:
12211224
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
@@ -592,6 +592,22 @@ def test_unicode(self):
592592
expected = 'unicode' if PY2 else 'string'
593593
assert result == expected
594594

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

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

0 commit comments

Comments
 (0)