Skip to content

ENH: Add the decimal.Decimal type to infer_dtypes (#15690) #16426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.python.org/3/library/pickle.html#data-stream-format>`__
- ``lib.infer_dtype`` now infers decimals. (:issue: `15690`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:func:`api.types.infer_dtype`


.. _whatsnew_0210.api_breaking:

Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ def infer_dtype(object value):
'categorical'

"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls update the doc-string & add an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


cdef:
Py_ssize_t i, n
object val
Expand Down Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment with a link to the github issue? (#15690)

result = lib.infer_dtype(arr)
assert result == 'decimal'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, could you add a test with a mix of Decimal and non-decimal (like floats) and make sure that the return is mixed? Or search through and see if we have a test that already covers this. Other than that, this looks great.

arr = np.array([1.0, 2.0, Decimal(3)])
result = lib.infer_dtype(arr)
assert result == 'mixed'

def test_string(self):
pass

Expand Down