Skip to content

WIP: Use dispatch_to_series for combine_const #22751

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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
32 changes: 28 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4867,10 +4867,34 @@ def _combine_const(self, other, func, errors='raise', try_cast=True):
if lib.is_scalar(other) or np.ndim(other) == 0:
return ops.dispatch_to_series(self, other, func)

new_data = self._data.eval(func=func, other=other,
errors=errors,
try_cast=try_cast)
return self._constructor(new_data)
elif (np.ndim(other) == 1 and isinstance(other, np.ndarray) and
len(other) == len(self.columns)):
right = np.broadcast_to(other, self.shape)
return ops.dispatch_to_series(self, right, func)

elif (np.ndim(other) == 1 and
isinstance(other, (tuple, np.ndarray)) and
Copy link
Member Author

Choose a reason for hiding this comment

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

There is a test for that goes through this path with a tuple, but none with a list. IIRC, no tests fail if list is included here.

Similarly, in the case above, only np.ndarray cases are tested. Should tuple and/or list be allowed?

len(other) == len(self) != len(self.columns)):
# tests include at least 1 tuple in this case
right = np.array(other)[:, None]
right = np.broadcast_to(right, self.shape)
return ops.dispatch_to_series(self, right, func)

elif np.ndim(other) == 1:
raise ValueError("Shape incompatible")

elif np.ndim(other) == 2 and other.shape == self.shape:
return ops.dispatch_to_series(self, other, func)

elif (np.ndim(other) == 2 and isinstance(other, np.ndarray) and
other.shape[0] == 1 and other.shape[1] == len(self.columns)):
Copy link
Member Author

Choose a reason for hiding this comment

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

Should the transposed case be supported?

other = np.broadcast_to(other, self.shape)
return ops.dispatch_to_series(self, other, func)

elif np.ndim(other) > 2:
raise ValueError("Wrong number of dimensions", other.shape)

raise ValueError(getattr(other, 'shape', type(other)))

def combine(self, other, func, fill_value=None, overwrite=True):
"""
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,31 @@ def column_op(a, b):
return {i: func(a.iloc[:, i], b)
for i in range(len(a.columns))}

elif np.ndim(right) == 2 and right.shape == left.shape:
# ndarray with same shape

def column_op(a, b):
return {i: func(a.iloc[:, i], b[:, i])
for i in range(len(a.columns))}

elif (np.ndim(right) == 2 and
right.shape[0] == 1 and
right.shape[1] == len(left.columns)):
# operate row-by-row

def column_op(a, b):
return {i: func(a.iloc[:, i], b[0, i])
for i in range(len(a.columns))}

elif (np.ndim(right) == 2 and
right.shape[1] == 1 and
right.shape[0] == len(left.index)):
# operate column-by-column

def column_op(a, b):
return {i: func(a.iloc[:, i], b[:, 0])
for i in range(len(a.columns))}

else:
# Remaining cases have less-obvious dispatch rules
raise NotImplementedError(right)
Expand Down