Skip to content

Commit c8bd2b9

Browse files
committed
API: fix corner case of lib.infer_dtype (pandas-dev#23422)
1 parent 58a200a commit c8bd2b9

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ Backwards incompatible API changes
246246
- :meth:`Series.str.cat` will now raise if `others` is a `set` (:issue:`23009`)
247247
- The `.str`-accessor will perform more rigorous type checking for inputs. Previously, some types that were never intended to be used
248248
"worked" purely due to limitations of dtype checking -- e.g. ``bytes``, which is now disabled except for :meth:`Series.str.decode` (:issue:`23011`, :issue:`23163`)
249+
- The method `pandas._libs.lib.infer_dtype` now returns `'empty'` rather than (sometimes) the dtype of the array,
250+
in case the array only consists of missing values and `skipna=True` (:issue:`23421`)
249251

250252
.. _whatsnew_0240.api_breaking.deps:
251253

pandas/_libs/lib.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ from tslibs.timezones cimport get_timezone, tz_compare
5959

6060
from missing cimport (checknull,
6161
is_null_datetime64, is_null_timedelta64, is_null_period)
62+
from missing import isnaobj
6263

6364

6465
# constants that will be compared to potentially arbitrarily large
@@ -1171,6 +1172,9 @@ def infer_dtype(object value, bint skipna=False):
11711172
values = construct_1d_object_array_from_listlike(value)
11721173

11731174
values = getattr(values, 'values', values)
1175+
if skipna:
1176+
values = values[~isnaobj(values)]
1177+
11741178
val = _try_infer_map(values)
11751179
if val is not None:
11761180
return val

pandas/tests/dtypes/test_inference.py

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

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

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

0 commit comments

Comments
 (0)