|
2 | 2 | import locale
|
3 | 3 | import codecs
|
4 | 4 | import nose
|
| 5 | +import decimal |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | from numpy import iinfo
|
@@ -208,6 +209,46 @@ def test_numeric(self):
|
208 | 209 | res = to_numeric(s)
|
209 | 210 | tm.assert_series_equal(res, expected)
|
210 | 211 |
|
| 212 | + # GH 14827 |
| 213 | + df = pd.DataFrame(dict( |
| 214 | + a=[1.2, decimal.Decimal(3.14), decimal.Decimal("infinity"), '0.1'], |
| 215 | + b=[1.0, 2.0, 3.0, 4.0], |
| 216 | + )) |
| 217 | + expected = pd.DataFrame(dict( |
| 218 | + a=[1.2, 3.14, np.inf, 0.1], |
| 219 | + b=[1.0, 2.0, 3.0, 4.0], |
| 220 | + )) |
| 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) |
| 231 | + |
| 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 | + |
211 | 252 | def test_all_nan(self):
|
212 | 253 | s = pd.Series(['a', 'b', 'c'])
|
213 | 254 | res = to_numeric(s, errors='coerce')
|
|
0 commit comments