Skip to content

Commit 51cba53

Browse files
committed
Merge pull request #11131 from jreback/error
ERR: make sure raising TypeError on invalid nanops reductions xref #10472
2 parents 7512f95 + b59d06c commit 51cba53

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ Other API Changes
915915
- ``groupby`` using ``Categorical`` follows the same rule as ``Categorical.unique`` described above (:issue:`10508`)
916916
- When constructing ``DataFrame`` with an array of ``complex64`` dtype previously meant the corresponding column
917917
was automatically promoted to the ``complex128`` dtype. Pandas will now preserve the itemsize of the input for complex data (:issue:`10952`)
918+
- some numeric reduction operators would return ``ValueError``, rather than ``TypeError`` on object types that includes strings and numbers (:issue:`11131`)
918919

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

pandas/core/nanops.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ def _f(*args, **kwargs):
4343
raise TypeError('reduction operation {0!r} not allowed for '
4444
'this dtype'.format(f.__name__.replace('nan',
4545
'')))
46-
return f(*args, **kwargs)
46+
try:
47+
return f(*args, **kwargs)
48+
except ValueError as e:
49+
# we want to transform an object array
50+
# ValueError message to the more typical TypeError
51+
# e.g. this is normally a disallowed function on
52+
# object arrays that contain strings
53+
if is_object_dtype(args[0]):
54+
raise TypeError(e)
55+
raise
4756
return _f
4857

4958

@@ -93,7 +102,17 @@ def f(values, axis=None, skipna=True, **kwds):
93102
else:
94103
result = alt(values, axis=axis, skipna=skipna, **kwds)
95104
except Exception:
96-
result = alt(values, axis=axis, skipna=skipna, **kwds)
105+
try:
106+
result = alt(values, axis=axis, skipna=skipna, **kwds)
107+
except ValueError as e:
108+
# we want to transform an object array
109+
# ValueError message to the more typical TypeError
110+
# e.g. this is normally a disallowed function on
111+
# object arrays that contain strings
112+
113+
if is_object_dtype(values):
114+
raise TypeError(e)
115+
raise
97116

98117
return result
99118

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

375-
376394
# xref GH10242
377395
# Compute variance via two-pass algorithm, which is stable against
378396
# cancellation errors and relatively accurate for small numbers of

pandas/tests/test_frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12792,7 +12792,7 @@ def test_numeric_only_flag(self):
1279212792

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

1279712797
def test_sem(self):
1279812798
alt = lambda x: np.std(x, ddof=1)/np.sqrt(len(x))

pandas/tests/test_series.py

+4
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,10 @@ def testit():
29122912
exp = alternate(s)
29132913
self.assertEqual(res, exp)
29142914

2915+
# check on string data
2916+
if name not in ['sum','min','max']:
2917+
self.assertRaises(TypeError, f, Series(list('abc')))
2918+
29152919
# Invalid axis.
29162920
self.assertRaises(ValueError, f, self.series, axis=1)
29172921

0 commit comments

Comments
 (0)