From a4a2933117c493394a5656c887a22ea02e94093f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 2 Oct 2018 11:24:48 -0500 Subject: [PATCH] handle test --- pandas/core/arrays/base.py | 4 +-- .../decimal/decimal_array/test_decimal.py | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 7bf13fb2fecc0..9f515c38c14ce 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -777,8 +777,8 @@ def convert_values(param): if coerce_to_dtype: try: res = self._from_sequence(res) - except TypeError: - pass + except Exception: + res = np.asarray(res, dtype=object) return res diff --git a/pandas/tests/extension/decimal/decimal_array/test_decimal.py b/pandas/tests/extension/decimal/decimal_array/test_decimal.py index c7a39ce3ff9f5..024f758943649 100644 --- a/pandas/tests/extension/decimal/decimal_array/test_decimal.py +++ b/pandas/tests/extension/decimal/decimal_array/test_decimal.py @@ -278,17 +278,32 @@ def test_compare_array(self, data, all_compare_operators): self._compare_other(s, data, op_name, other) +class DecimalArrayWithoutFromSequence(DecimalArray): + """Helper class for testing error handling in _from_sequence.""" + def _from_sequence(cls, scalars, dtype=None, copy=False): + raise KeyError("For the test") + + def test_combine_from_sequence_raises(): # https://github.com/pandas-dev/pandas/issues/22850 - class BadDecimalArray(DecimalArray): - def _from_sequence(cls, scalars, dtype=None, copy=False): - raise KeyError("For the test") - - ser = pd.Series(BadDecimalArray([decimal.Decimal("1.0"), - decimal.Decimal("2.0")])) + ser = pd.Series(DecimalArrayWithoutFromSequence([ + decimal.Decimal("1.0"), + decimal.Decimal("2.0") + ])) result = ser.combine(ser, operator.add) # note: object dtype expected = pd.Series([decimal.Decimal("2.0"), decimal.Decimal("4.0")], dtype="object") tm.assert_series_equal(result, expected) + + +def test_scalar_ops_from_sequence_raises(): + arr = DecimalArrayWithoutFromSequence([ + decimal.Decimal("1.0"), + decimal.Decimal("2.0") + ]) + result = arr + arr + expected = np.array([decimal.Decimal("2.0"), decimal.Decimal("4.0")], + dtype="object") + tm.assert_numpy_array_equal(result, expected)