Skip to content

Commit 8eecb6c

Browse files
author
Roger Thomas
committed
Fix To Numeric on Decimal Fields
1 parent 5f057cb commit 8eecb6c

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ Performance Improvements
109109

110110
Bug Fixes
111111
~~~~~~~~~
112+
113+
- Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`)

pandas/tools/tests/test_util.py

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import locale
33
import codecs
44
import nose
5+
import decimal
56

67
import numpy as np
78
from numpy import iinfo
@@ -208,6 +209,16 @@ def test_numeric(self):
208209
res = to_numeric(s)
209210
tm.assert_series_equal(res, expected)
210211

212+
# GH 14827
213+
df = pd.DataFrame(dict(
214+
a=[1.2, decimal.Decimal(3.14), decimal.Decimal("infinity"), '0.1']
215+
))
216+
df['a'] = df['a'].apply(to_numeric)
217+
expected = pd.DataFrame(dict(
218+
a=[1.2, 3.14, np.inf, 0.1]
219+
))
220+
tm.assert_frame_equal(df, expected)
221+
211222
def test_all_nan(self):
212223
s = pd.Series(['a', 'b', 'c'])
213224
res = to_numeric(s, errors='coerce')

pandas/tools/util.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from decimal import Decimal
12
import numpy as np
23
import pandas.lib as lib
34

@@ -173,6 +174,8 @@ def to_numeric(arg, errors='raise', downcast=None):
173174
values = arg.values
174175
elif isinstance(arg, (list, tuple)):
175176
values = np.array(arg, dtype='O')
177+
elif isinstance(arg, Decimal):
178+
return float(arg)
176179
elif np.isscalar(arg):
177180
if is_number(arg):
178181
return arg

0 commit comments

Comments
 (0)