Skip to content

Commit f3d7c18

Browse files
Roger Thomasjreback
Roger Thomas
authored andcommitted
BUG: Fix maybe_convert_numeric for unhashable objects
closes pandas-dev#13324 Author: Roger Thomas <[email protected]> Closes pandas-dev#13326 from RogerThomas/fix_maybe_convert_numeric_for_unhashable_objects and squashes the following commits: 76a0738 [Roger Thomas] Fix maybe_convert_numeric for unhashable objects
1 parent d191640 commit f3d7c18

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

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

370370

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

372373

373374
- 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

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ 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+
# GH13324
107+
# make sure that we are handing non-hashables
108+
arr = np.array([[10.0, 2], 1.0, 'apple'])
109+
result = lib.maybe_convert_numeric(arr, set(), False, True)
110+
tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan]))
111+
105112

106113
class TestTypeInference(tm.TestCase):
107114
_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)