Skip to content

Commit

Permalink
ERR: make sure raising TypeError on invalid nanops reductions xref #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Sep 16, 2015
1 parent 7512f95 commit b59d06c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ Other API Changes
- ``groupby`` using ``Categorical`` follows the same rule as ``Categorical.unique`` described above (:issue:`10508`)
- When constructing ``DataFrame`` with an array of ``complex64`` dtype previously meant the corresponding column
was automatically promoted to the ``complex128`` dtype. Pandas will now preserve the itemsize of the input for complex data (:issue:`10952`)
- some numeric reduction operators would return ``ValueError``, rather than ``TypeError`` on object types that includes strings and numbers (:issue:`11131`)

- ``NaT``'s methods now either raise ``ValueError``, or return ``np.nan`` or ``NaT`` (:issue:`9513`)

Expand Down
24 changes: 21 additions & 3 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ def _f(*args, **kwargs):
raise TypeError('reduction operation {0!r} not allowed for '
'this dtype'.format(f.__name__.replace('nan',
'')))
return f(*args, **kwargs)
try:
return f(*args, **kwargs)
except ValueError as e:
# we want to transform an object array
# ValueError message to the more typical TypeError
# e.g. this is normally a disallowed function on
# object arrays that contain strings
if is_object_dtype(args[0]):
raise TypeError(e)
raise
return _f


Expand Down Expand Up @@ -93,7 +102,17 @@ def f(values, axis=None, skipna=True, **kwds):
else:
result = alt(values, axis=axis, skipna=skipna, **kwds)
except Exception:
result = alt(values, axis=axis, skipna=skipna, **kwds)
try:
result = alt(values, axis=axis, skipna=skipna, **kwds)
except ValueError as e:
# we want to transform an object array
# ValueError message to the more typical TypeError
# e.g. this is normally a disallowed function on
# object arrays that contain strings

if is_object_dtype(values):
raise TypeError(e)
raise

return result

Expand Down Expand Up @@ -372,7 +391,6 @@ def nanvar(values, axis=None, skipna=True, ddof=1):
values = values.copy()
np.putmask(values, mask, 0)


# xref GH10242
# Compute variance via two-pass algorithm, which is stable against
# cancellation errors and relatively accurate for small numbers of
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12792,7 +12792,7 @@ def test_numeric_only_flag(self):

# df1 has all numbers, df2 has a letter inside
self.assertRaises(TypeError, lambda : getattr(df1, meth)(axis=1, numeric_only=False))
self.assertRaises(ValueError, lambda : getattr(df2, meth)(axis=1, numeric_only=False))
self.assertRaises(TypeError, lambda : getattr(df2, meth)(axis=1, numeric_only=False))

def test_sem(self):
alt = lambda x: np.std(x, ddof=1)/np.sqrt(len(x))
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,10 @@ def testit():
exp = alternate(s)
self.assertEqual(res, exp)

# check on string data
if name not in ['sum','min','max']:
self.assertRaises(TypeError, f, Series(list('abc')))

# Invalid axis.
self.assertRaises(ValueError, f, self.series, axis=1)

Expand Down

0 comments on commit b59d06c

Please sign in to comment.