8000 Catch Exception in combine by TomAugspurger · Pull Request #22936 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000 File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Closes #22850
  • Loading branch information
TomAugspurger committed Oct 2, 2018
commit fdd43c4de98992a6f97a835fdbb525f829ef1d69
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
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)
0