Skip to content

BUG: Fix maybe_convert_numeric for unhashable objects #13326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Bug Fixes
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)


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


- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
2 changes: 1 addition & 1 deletion pandas/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def maybe_convert_numeric(object[:] values, set na_values,
for i in range(n):
val = values[i]

if val in na_values:
if val.__hash__ is not None and val in na_values:
floats[i] = complexes[i] = nan
seen_float = True
elif util.is_float_object(val):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_infer_and_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def test_scientific_no_exponent(self):
result = lib.maybe_convert_numeric(arr, set(), False, True)
self.assertTrue(np.all(np.isnan(result)))

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


class TestTypeInference(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down
12 changes: 12 additions & 0 deletions pandas/tools/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ def test_period(self):
# res = pd.to_numeric(pd.Series(idx, name='xxx'))
# tm.assert_series_equal(res, pd.Series(idx.asi8, name='xxx'))

def test_non_hashable(self):
# Test for Bug #13324
s = pd.Series([[10.0, 2], 1.0, 'apple'])
res = pd.to_numeric(s, errors='coerce')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run thru ignore, raise as well

tm.assert_series_equal(res, pd.Series([np.nan, 1.0, np.nan]))

res = pd.to_numeric(s, errors='ignore')
tm.assert_series_equal(res, pd.Series([[10.0, 2], 1.0, 'apple']))

with self.assertRaisesRegexp(TypeError, "Invalid object type"):
pd.to_numeric(s)


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