Skip to content

Commit 1f1c62c

Browse files
author
Roger Thomas
committed
Add Test And Refactor is_decimal
1 parent f1b69da commit 1f1c62c

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

pandas/lib.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def isscalar(object val):
313313
- instances of datetime.datetime
314314
- instances of datetime.timedelta
315315
- Period
316+
- instances of decimal.Decimal
316317
317318
"""
318319

@@ -325,7 +326,8 @@ def isscalar(object val):
325326
or PyDate_Check(val)
326327
or PyDelta_Check(val)
327328
or PyTime_Check(val)
328-
or util.is_period_object(val))
329+
or util.is_period_object(val)
330+
or is_decimal(val))
329331

330332

331333
def item_from_zerodim(object val):

pandas/src/inference.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from decimal import Decimal
21
import sys
2+
from decimal import Decimal
33
cimport util
44
from tslib import NaT, get_timezone
55
from datetime import datetime, timedelta
@@ -29,6 +29,10 @@ def is_bool(object obj):
2929
def is_complex(object obj):
3030
return util.is_complex_object(obj)
3131

32+
33+
def is_decimal(object obj):
34+
return isinstance(obj, Decimal)
35+
3236
cpdef bint is_period(object val):
3337
""" Return a boolean if this is a Period object """
3438
return util.is_period_object(val)
@@ -674,7 +678,7 @@ def maybe_convert_numeric(object[:] values, set na_values,
674678
elif util.is_complex_object(val):
675679
complexes[i] = val
676680
seen_complex = True
677-
elif isinstance(val, Decimal):
681+
elif is_decimal(val):
678682
floats[i] = complexes[i] = val
679683
seen_float = True
680684
else:

pandas/tools/tests/test_util.py

+20
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,26 @@ def test_numeric(self):
229229
df_copy[['a', 'b']] = df_copy[['a', 'b']].apply(to_numeric)
230230
tm.assert_frame_equal(df_copy, expected)
231231

232+
def test_numeric_lists_and_arrays(self):
233+
# Test to_numeric with embedded lists and arrays
234+
df = pd.DataFrame(dict(
235+
a=[[decimal.Decimal(3.14), 1.0], decimal.Decimal(1.6), 0.1]
236+
))
237+
df['a'] = df['a'].apply(to_numeric)
238+
expected = pd.DataFrame(dict(
239+
a=[[3.14, 1.0], 1.6, 0.1],
240+
))
241+
tm.assert_frame_equal(df, expected)
242+
243+
df = pd.DataFrame(dict(
244+
a=[np.array([decimal.Decimal(3.14), 1.0]), 0.1]
245+
))
246+
df['a'] = df['a'].apply(to_numeric)
247+
expected = pd.DataFrame(dict(
248+
a=[[3.14, 1.0], 0.1],
249+
))
250+
tm.assert_frame_equal(df, expected)
251+
232252
def test_all_nan(self):
233253
s = pd.Series(['a', 'b', 'c'])
234254
res = to_numeric(s, errors='coerce')

pandas/tools/util.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from decimal import Decimal
21
import numpy as np
32
import pandas.lib as lib
43

@@ -7,6 +6,7 @@
76
is_datetime_or_timedelta_dtype,
87
is_list_like,
98
_ensure_object)
9+
1010
from pandas.types.cast import _possibly_downcast_to_dtype
1111

1212
import pandas as pd
@@ -174,9 +174,9 @@ def to_numeric(arg, errors='raise', downcast=None):
174174
values = arg.values
175175
elif isinstance(arg, (list, tuple)):
176176
values = np.array(arg, dtype='O')
177-
elif isinstance(arg, Decimal):
178-
return float(arg)
179-
elif np.isscalar(arg):
177+
elif lib.isscalar(arg):
178+
if lib.is_decimal(arg):
179+
return float(arg)
180180
if is_number(arg):
181181
return arg
182182
is_scalar = True

0 commit comments

Comments
 (0)