Skip to content

Commit 3996fe8

Browse files
author
Roger Thomas
committed
Add test for multiple
1 parent 8eecb6c commit 3996fe8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pandas/src/inference.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from decimal import Decimal
12
import sys
23
cimport util
34
from tslib import NaT, get_timezone
@@ -673,6 +674,9 @@ def maybe_convert_numeric(object[:] values, set na_values,
673674
elif util.is_complex_object(val):
674675
complexes[i] = val
675676
seen_complex = True
677+
elif isinstance(val, Decimal):
678+
floats[i] = complexes[i] = val
679+
seen_float = True
676680
else:
677681
try:
678682
status = floatify(val, &fval, &maybe_int)

pandas/tools/tests/test_util.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,23 @@ def test_numeric(self):
211211

212212
# GH 14827
213213
df = pd.DataFrame(dict(
214-
a=[1.2, decimal.Decimal(3.14), decimal.Decimal("infinity"), '0.1']
214+
a=[1.2, decimal.Decimal(3.14), decimal.Decimal("infinity"), '0.1'],
215+
b=[1.0, 2.0, 3.0, 4.0],
215216
))
216-
df['a'] = df['a'].apply(to_numeric)
217217
expected = pd.DataFrame(dict(
218-
a=[1.2, 3.14, np.inf, 0.1]
218+
a=[1.2, 3.14, np.inf, 0.1],
219+
b=[1.0, 2.0, 3.0, 4.0],
219220
))
220-
tm.assert_frame_equal(df, expected)
221+
222+
# Test to_numeric over one column
223+
df_copy = df.copy()
224+
df_copy['a'] = df_copy['a'].apply(to_numeric)
225+
tm.assert_frame_equal(df_copy, expected)
226+
227+
# Test to_numeric over multiple columns
228+
df_copy = df.copy()
229+
df_copy[['a', 'b']] = df_copy[['a', 'b']].apply(to_numeric)
230+
tm.assert_frame_equal(df_copy, expected)
221231

222232
def test_all_nan(self):
223233
s = pd.Series(['a', 'b', 'c'])

0 commit comments

Comments
 (0)