From bc15e857c84db313dab4aac3d46f02f2398607ea Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 11:48:26 -0700 Subject: [PATCH 1/6] ENH: Add the decimal.Decimal type to infer_dtypes (#15690) --- pandas/_libs/src/inference.pyx | 4 +++- pandas/tests/dtypes/test_inference.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/src/inference.pyx b/pandas/_libs/src/inference.pyx index ddd38979e326c..e1d0f987008f1 100644 --- a/pandas/_libs/src/inference.pyx +++ b/pandas/_libs/src/inference.pyx @@ -308,7 +308,6 @@ def infer_dtype(object value): 'categorical' """ - cdef: Py_ssize_t i, n object val @@ -407,6 +406,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..318300c8ca84d 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,11 @@ def test_floats(self): result = lib.infer_dtype(arr) assert result == 'floating' + def test_decimals(self): + arr = np.array([Decimal(1), Decimal(2), Decimal(3)]) + result = lib.infer_dtype(arr) + assert result == 'decimal' + def test_string(self): pass From e7e67e8efe09471a03619b378e10d1e53f4f548f Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 12:16:40 -0700 Subject: [PATCH 2/6] Reference GH15690 in test_decimals --- pandas/tests/dtypes/test_inference.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 318300c8ca84d..e404784c179bd 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -464,6 +464,7 @@ def test_floats(self): 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' From b291b6215aec3efb65df73482bc4fe3afd881639 Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 12:20:54 -0700 Subject: [PATCH 3/6] Add enhancement from issue #15690 --- doc/source/whatsnew/v0.21.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 3734dc15be2e9..bf860b8ffe973 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 `__ +- ``lib.infer_dtype`` now infers decimals. (:issue: `15690`) .. _whatsnew_0210.api_breaking: From 024feac23aa8df3cae8b23ef7ea6ce97e69c4ae6 Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 12:32:10 -0700 Subject: [PATCH 4/6] Check that decimal and non-decimal returns 'mixed' --- pandas/tests/dtypes/test_inference.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index e404784c179bd..b88481abcb2ec 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -469,6 +469,10 @@ def test_decimals(self): 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 From cc9ac39b9f312c0c13052c057a3cb73aea081b01 Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 15:26:33 -0700 Subject: [PATCH 5/6] Fix reference to infer_dtype func --- doc/source/whatsnew/v0.21.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index bf860b8ffe973..857f1aaf48b42 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -34,7 +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 `__ -- ``lib.infer_dtype`` now infers decimals. (:issue: `15690`) +- :func:`api.types.infer_dtype` now infers decimals. (:issue: `15690`) .. _whatsnew_0210.api_breaking: From 6f158cb23cbfc51b35f9109a65e2929721f1e9a2 Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 15:26:59 -0700 Subject: [PATCH 6/6] update infer_dtype docstring with decimal example --- pandas/_libs/src/inference.pyx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/_libs/src/inference.pyx b/pandas/_libs/src/inference.pyx index e1d0f987008f1..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'