Skip to content

Commit 82e21a3

Browse files
margaretstangirala
authored andcommitted
ENH: Add the decimal.Decimal type to infer_dtypes (pandas-dev#15690) (pandas-dev#16426)
closes pandas-dev#15690
1 parent a74cdb3 commit 82e21a3

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Other Enhancements
3434
- ``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`)
3535
- ``RangeIndex.append`` now returns a ``RangeIndex`` object when possible (:issue:`16212`)
3636
- :func:`to_pickle` has gained a protocol parameter (:issue:`16252`). By default, this parameter is set to `HIGHEST_PROTOCOL <https://docs.python.org/3/library/pickle.html#data-stream-format>`__
37+
- :func:`api.types.infer_dtype` now infers decimals. (:issue: `15690`)
3738

3839
.. _whatsnew_0210.api_breaking:
3940

pandas/_libs/src/inference.pyx

+7-1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def infer_dtype(object value):
243243
- integer
244244
- mixed-integer
245245
- mixed-integer-float
246+
- decimal
246247
- complex
247248
- categorical
248249
- boolean
@@ -286,6 +287,9 @@ def infer_dtype(object value):
286287
>>> infer_dtype(['a', 1])
287288
'mixed-integer'
288289
290+
>>> infer_dtype([Decimal(1), Decimal(2.0)])
291+
'decimal'
292+
289293
>>> infer_dtype([True, False])
290294
'boolean'
291295
@@ -308,7 +312,6 @@ def infer_dtype(object value):
308312
'categorical'
309313
310314
"""
311-
312315
cdef:
313316
Py_ssize_t i, n
314317
object val
@@ -407,6 +410,9 @@ def infer_dtype(object value):
407410
if is_time_array(values):
408411
return 'time'
409412

413+
elif is_decimal(val):
414+
return 'decimal'
415+
410416
elif util.is_float_object(val):
411417
if is_float_array(values):
412418
return 'floating'

pandas/tests/dtypes/test_inference.py

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import collections
1010
import re
1111
from datetime import datetime, date, timedelta, time
12+
from decimal import Decimal
1213
import numpy as np
1314
import pytz
1415
import pytest
@@ -462,6 +463,16 @@ def test_floats(self):
462463
result = lib.infer_dtype(arr)
463464
assert result == 'floating'
464465

466+
def test_decimals(self):
467+
# GH15690
468+
arr = np.array([Decimal(1), Decimal(2), Decimal(3)])
469+
result = lib.infer_dtype(arr)
470+
assert result == 'decimal'
471+
472+
arr = np.array([1.0, 2.0, Decimal(3)])
473+
result = lib.infer_dtype(arr)
474+
assert result == 'mixed'
475+
465476
def test_string(self):
466477
pass
467478

0 commit comments

Comments
 (0)