Skip to content

Avoid listifying left/right operand in ExtensionArray ops dispatching #22922

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

Closed
jorisvandenbossche opened this issue Oct 1, 2018 · 8 comments · Fixed by #23155
Closed

Avoid listifying left/right operand in ExtensionArray ops dispatching #22922

jorisvandenbossche opened this issue Oct 1, 2018 · 8 comments · Fixed by #23155
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Milestone

Comments

@jorisvandenbossche
Copy link
Member

We now have the follow code in the ExtensionArray ops dispatching:

pandas/pandas/core/ops.py

Lines 1169 to 1192 in 2f1b842

# TODO(jreback)
# we need to listify to avoid ndarray, or non-same-type extension array
# dispatching
if is_extension_array_dtype(left):
new_left = left.values
if isinstance(right, np.ndarray):
# handle numpy scalars, this is a PITA
# TODO(jreback)
new_right = lib.item_from_zerodim(right)
if is_scalar(new_right):
new_right = [new_right]
new_right = list(new_right)
elif is_extension_array_dtype(right) and type(left) != type(right):
new_right = list(new_right)
else:
new_right = right
else:
new_left = list(left.values)
new_right = right

There are a few places where we convert the values to a list, which should be avoided.

@jorisvandenbossche jorisvandenbossche added the ExtensionArray Extending pandas with custom dtypes or arrays. label Oct 1, 2018
@jorisvandenbossche jorisvandenbossche added this to the Contributions Welcome milestone Oct 1, 2018
@TomAugspurger
Copy link
Contributor

This specific case of Series[EA] + EA (where the EAs are the same type) seems very easy to handle.

@jorisvandenbossche
Copy link
Member Author

Looking at the code, it seems that that case is already working OK, if the type(EA) is equal.

@TomAugspurger
Copy link
Contributor

Hmm is it? I thought that's what cased the NameError. I think we need something like type(new_left) is type(right), where new_left has unboxed the EA from the Series. Haven't looked closely though.

@jorisvandenbossche
Copy link
Member Author

Yes, it's what caused the NameError, but this is about the conversion to list?

@TomAugspurger
Copy link
Contributor

Correct. I'm saying we shouldn't even reach that elif in the case of Series[EA] + EA.

elif is_extension_array_dtype(right) and type(new_left) == type(right):
    # EA + EA. This is new.
    new_right = right
elif is_extension_array_dtype(right) and type(left) != type(right):
    new_right = list(right)

@TomAugspurger
Copy link
Contributor

Corner case in pandas master with object-dtype

In [1]: import pandas as pd

In [2]: 1 ** pd.Series([None], dtype=object)
Out[2]:
0    NaN
dtype: object

That's supposed to be 1, since 1 ** anything is 1

In [4]: 1 ** pd.Series([np.nan])
Out[4]:
0    1.0
dtype: float64

@jorisvandenbossche
Copy link
Member Author

@TomAugspurger is that related to the listifying?

@TomAugspurger
Copy link
Contributor

Whoops, got my issues mixed up. That was for #22022

@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants