Skip to content

Catch Exception in combine #22936

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

Merged
merged 15 commits into from
Oct 4, 2018
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ ExtensionType Changes
- :meth:`Series.astype` and :meth:`DataFrame.astype` now dispatch to :meth:`ExtensionArray.astype` (:issue:`21185:`).
- Slicing a single row of a ``DataFrame`` with multiple ExtensionArrays of the same type now preserves the dtype, rather than coercing to object (:issue:`22784`)
- Added :meth:`pandas.api.types.register_extension_dtype` to register an extension type with pandas (:issue:`22664`)
- Renamed test prototype module names to avoid conflict with modules from the standard library (:issue:`22936`)
Copy link
Contributor

Choose a reason for hiding this comment

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

not clear what is changing here

Copy link
Contributor Author

Choose a reason for hiding this comment

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


.. _whatsnew_0240.api.incompatibilities:

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2324,9 +2324,11 @@ def combine(self, other, func, fill_value=None):
elif is_extension_array_dtype(self.values):
# The function can return something of any type, so check
# if the type is compatible with the calling EA
# ExtensionArray._from_sequence can raise anything, so we
# have to catch everything.
try:
new_values = self._values._from_sequence(new_values)
except TypeError:
except Exception:
pass

return self._constructor(new_values, index=new_index, name=new_name)
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions pandas/tests/extension/decimal_array/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .array import DecimalArray, DecimalDtype


__all__ = ['DecimalArray', 'DecimalDtype']
Copy link
Member

Choose a reason for hiding this comment

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

can you add here make_data ? (that are typically the ones that I use to test things out locally)

Empty file.
3 changes: 3 additions & 0 deletions pandas/tests/extension/json_array/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .array import JSONArray, JSONDtype

__all__ = ['JSONArray', 'JSONDtype']
20 changes: 20 additions & 0 deletions pandas/tests/extension/test_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import operator
from decimal import Decimal

import pandas as pd
import pandas.util.testing as tm
from pandas.tests.extension.decimal_array import DecimalArray


def test_combine_from_sequence_raises():
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this not with the existomg tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't a base extensionarray test that applies to all EAs. It's just to EAs that may raise errors other than TypeError in _values_from_sequence.

We need a place for simple tests like this that

  1. Aren't applicable to every downstream ExtensionArray (so not in tests/extension/base)
  2. Are testing pandas dispatching, rather than the behavior of a specific EA (so not in tests/arrays/)

This seemed like a sensible home.

# 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("1.0"), Decimal("2.0")]))
result = ser.combine(ser, operator.add)

# note: object dtype
expected = pd.Series([Decimal("2.0"), Decimal("4.0")], dtype="object")
tm.assert_series_equal(result, expected)