Skip to content

Commit

Permalink
API: fix corner case of lib.infer_dtype (#23422)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Oct 30, 2018
1 parent 58a200a commit c8bd2b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Backwards incompatible API changes
- :meth:`Series.str.cat` will now raise if `others` is a `set` (:issue:`23009`)
- The `.str`-accessor will perform more rigorous type checking for inputs. Previously, some types that were never intended to be used
"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`)
- The method `pandas._libs.lib.infer_dtype` now returns `'empty'` rather than (sometimes) the dtype of the array,
in case the array only consists of missing values and `skipna=True` (:issue:`23421`)

.. _whatsnew_0240.api_breaking.deps:

Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ from tslibs.timezones cimport get_timezone, tz_compare

from missing cimport (checknull,
is_null_datetime64, is_null_timedelta64, is_null_period)
from missing import isnaobj


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

values = getattr(values, 'values', values)
if skipna:
values = values[~isnaobj(values)]

val = _try_infer_map(values)
if val is not None:
return val
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,19 @@ def test_unicode(self):
expected = 'unicode' if PY2 else 'string'
assert result == expected

@pytest.mark.parametrize('dtype, skipna, expected', [
(float, False, 'floating'),
(float, True, 'floating'),
(object, False, 'floating'),
(object, True, 'empty')
])
def test_object_empty(self, dtype, skipna, expected):
# GH 23421
arr = pd.Series([np.nan, np.nan], dtype=dtype)

result = lib.infer_dtype(arr, skipna=skipna)
assert result == expected

def test_datetime(self):

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

0 comments on commit c8bd2b9

Please sign in to comment.