From 791dd3f9939e4b84adf85f86b1fa503fafc50aea Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 19 Oct 2019 11:32:13 -0700 Subject: [PATCH] BUG: fix TypeError raised in maybe_downcast_numeric --- pandas/core/dtypes/cast.py | 2 +- pandas/core/groupby/generic.py | 2 +- pandas/tests/dtypes/cast/test_downcast.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index dd001e78c07de..7fcaf60088ad2 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -202,7 +202,7 @@ def trans(x): r = result.ravel() arr = np.array([r[0]]) - if isna(arr).any() or not np.allclose(arr, trans(arr).astype(dtype), rtol=0): + if isna(arr).any(): # if we have any nulls, then we are done return result diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index a78857423e7e0..7d89a08d3bad0 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -261,7 +261,7 @@ def aggregate(self, func=None, *args, **kwargs): try: return self._python_agg_general(func, *args, **kwargs) - except AssertionError: + except (AssertionError, TypeError): raise except Exception: result = self._aggregate_named(func, *args, **kwargs) diff --git a/pandas/tests/dtypes/cast/test_downcast.py b/pandas/tests/dtypes/cast/test_downcast.py index d574b03a8c724..9e2eca5259bc3 100644 --- a/pandas/tests/dtypes/cast/test_downcast.py +++ b/pandas/tests/dtypes/cast/test_downcast.py @@ -1,3 +1,5 @@ +import decimal + import numpy as np import pytest @@ -25,6 +27,13 @@ "infer", np.array([8, 8, 8, 8, 9], dtype=np.int64), ), + ( + # This is a judgement call, but we do _not_ downcast Decimal + # objects + np.array([decimal.Decimal(0.0)]), + "int64", + np.array([decimal.Decimal(0.0)]), + ), ], ) def test_downcast(arr, expected, dtype):