diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 3734dc15be2e9..857f1aaf48b42 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -34,6 +34,7 @@ Other Enhancements - ``Series.to_dict()`` and ``DataFrame.to_dict()`` now support an ``into`` keyword which allows you to specify the ``collections.Mapping`` subclass that you would like returned. The default is ``dict``, which is backwards compatible. (:issue:`16122`) - ``RangeIndex.append`` now returns a ``RangeIndex`` object when possible (:issue:`16212`) - :func:`to_pickle` has gained a protocol parameter (:issue:`16252`). By default, this parameter is set to `HIGHEST_PROTOCOL `__ +- :func:`api.types.infer_dtype` now infers decimals. (:issue: `15690`) .. _whatsnew_0210.api_breaking: diff --git a/pandas/_libs/src/inference.pyx b/pandas/_libs/src/inference.pyx index ddd38979e326c..38e95fe6ee652 100644 --- a/pandas/_libs/src/inference.pyx +++ b/pandas/_libs/src/inference.pyx @@ -243,6 +243,7 @@ def infer_dtype(object value): - integer - mixed-integer - mixed-integer-float + - decimal - complex - categorical - boolean @@ -286,6 +287,9 @@ def infer_dtype(object value): >>> infer_dtype(['a', 1]) 'mixed-integer' + >>> infer_dtype([Decimal(1), Decimal(2.0)]) + 'decimal' + >>> infer_dtype([True, False]) 'boolean' @@ -308,7 +312,6 @@ def infer_dtype(object value): 'categorical' """ - cdef: Py_ssize_t i, n object val @@ -407,6 +410,9 @@ def infer_dtype(object value): if is_time_array(values): return 'time' + elif is_decimal(val): + return 'decimal' + elif util.is_float_object(val): if is_float_array(values): return 'floating' diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 3790ebe0d3e7c..b88481abcb2ec 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -9,6 +9,7 @@ import collections import re from datetime import datetime, date, timedelta, time +from decimal import Decimal import numpy as np import pytz import pytest @@ -462,6 +463,16 @@ def test_floats(self): result = lib.infer_dtype(arr) assert result == 'floating' + def test_decimals(self): + # GH15690 + arr = np.array([Decimal(1), Decimal(2), Decimal(3)]) + result = lib.infer_dtype(arr) + assert result == 'decimal' + + arr = np.array([1.0, 2.0, Decimal(3)]) + result = lib.infer_dtype(arr) + assert result == 'mixed' + def test_string(self): pass