Skip to content

Commit 76a0738

Browse files
author
Roger Thomas
committed
Fix maybe_convert_numeric for unhashable objects
1 parent 9e7bfdd commit 76a0738

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Bug Fixes
317317
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)
318318

319319

320+
- Bug in ``pd.to_numeric`` when ``errors='coerce'`` and input contains non-hashable objects (:issue:`13324`)
320321

321322

322323
- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)

pandas/src/inference.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def maybe_convert_numeric(object[:] values, set na_values,
569569
for i in range(n):
570570
val = values[i]
571571

572-
if val in na_values:
572+
if val.__hash__ is not None and val in na_values:
573573
floats[i] = complexes[i] = nan
574574
seen_float = True
575575
elif util.is_float_object(val):

pandas/tests/test_infer_and_convert.py

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ def test_scientific_no_exponent(self):
102102
result = lib.maybe_convert_numeric(arr, set(), False, True)
103103
self.assertTrue(np.all(np.isnan(result)))
104104

105+
def test_convert_non_hashable(self):
106+
# Test for Bug #13324
107+
arr = np.array([[10.0, 2], 1.0, 'apple'])
108+
result = lib.maybe_convert_numeric(arr, set(), False, True)
109+
tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan]))
110+
105111

106112
class TestTypeInference(tm.TestCase):
107113
_multiprocess_can_split_ = True

pandas/tools/tests/test_util.py

+12
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ def test_period(self):
279279
# res = pd.to_numeric(pd.Series(idx, name='xxx'))
280280
# tm.assert_series_equal(res, pd.Series(idx.asi8, name='xxx'))
281281

282+
def test_non_hashable(self):
283+
# Test for Bug #13324
284+
s = pd.Series([[10.0, 2], 1.0, 'apple'])
285+
res = pd.to_numeric(s, errors='coerce')
286+
tm.assert_series_equal(res, pd.Series([np.nan, 1.0, np.nan]))
287+
288+
res = pd.to_numeric(s, errors='ignore')
289+
tm.assert_series_equal(res, pd.Series([[10.0, 2], 1.0, 'apple']))
290+
291+
with self.assertRaisesRegexp(TypeError, "Invalid object type"):
292+
pd.to_numeric(s)
293+
282294

283295
if __name__ == '__main__':
284296
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)