Skip to content

CLN: remove unused axis keyword from Block.where #40561

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 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 3 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8933,7 +8933,7 @@ def _where(
# align the cond to same shape as myself
cond = com.apply_if_callable(cond, self)
if isinstance(cond, NDFrame):
cond, _ = cond.align(self, join="right", broadcast_axis=1)
cond, _ = cond.align(self, join="right", broadcast_axis=1, copy=False)
else:
if not hasattr(cond, "shape"):
cond = np.asanyarray(cond)
Expand Down Expand Up @@ -8961,6 +8961,7 @@ def _where(
cond = cond.astype(bool)

cond = -cond if inplace else cond
cond = cond.reindex(self._info_axis, axis=self._info_axis_number, copy=False)

# try to align with other
if isinstance(other, NDFrame):
Expand Down Expand Up @@ -8997,7 +8998,7 @@ def _where(
"cannot align with a higher dimensional NDFrame"
)

if not isinstance(other, (MultiIndex, NDFrame)):
elif not isinstance(other, (MultiIndex, NDFrame)):
# mainly just catching Index here
other = extract_array(other, extract_numpy=True)

Expand Down Expand Up @@ -9029,11 +9030,6 @@ def _where(
else:
align = self._get_axis_number(axis) == 1

if isinstance(cond, NDFrame):
cond = cond.reindex(
self._info_axis, axis=self._info_axis_number, copy=False
)

if inplace:
# we may have different type blocks come out of putmask, so
# reconstruct the block manager
Expand All @@ -9049,7 +9045,6 @@ def _where(
cond=cond,
align=align,
errors=errors,
axis=axis,
)
result = self._constructor(new_data)
return result.__finalize__(self)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def quantile(
axes = [qs, self._axes[1]]
return type(self)(new_arrs, axes)

def where(self, other, cond, align: bool, errors: str, axis: int) -> ArrayManager:
def where(self, other, cond, align: bool, errors: str) -> ArrayManager:
if align:
align_keys = ["other", "cond"]
else:
Expand All @@ -534,7 +534,6 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> ArrayManage
other=other,
cond=cond,
errors=errors,
axis=axis,
)

# TODO what is this used for?
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo

return [self.make_block(new_values)]

def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
def where(self, other, cond, errors="raise") -> List[Block]:
"""
evaluate the block; return result block(s) from the result

Expand All @@ -1297,14 +1297,14 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
errors : str, {'raise', 'ignore'}, default 'raise'
- ``raise`` : allow exceptions to be raised
- ``ignore`` : suppress exceptions. On error return original object
axis : int, default 0

Returns
-------
List[Block]
"""
import pandas.core.computation.expressions as expressions

assert cond.ndim == self.ndim
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))

assert errors in ["raise", "ignore"]
Expand All @@ -1317,7 +1317,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:

icond, noop = validate_putmask(values, ~cond)

if is_valid_na_for_dtype(other, self.dtype) and not self.is_object:
if is_valid_na_for_dtype(other, self.dtype) and self.dtype != _dtype_obj:
other = self.fill_value

if noop:
Expand All @@ -1330,7 +1330,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
# we cannot coerce, return a compat dtype
# we are explicitly ignoring errors
block = self.coerce_to_target_dtype(other)
blocks = block.where(orig_other, cond, errors=errors, axis=axis)
blocks = block.where(orig_other, cond, errors=errors)
return self._maybe_downcast(blocks, "infer")

# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
Expand Down Expand Up @@ -1359,7 +1359,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
cond = ~icond
axis = cond.ndim - 1
cond = cond.swapaxes(axis, 0)
mask = np.array([cond[i].all() for i in range(cond.shape[0])], dtype=bool)
mask = cond.all(axis=1)

result_blocks: List[Block] = []
for m in [mask, ~mask]:
Expand Down Expand Up @@ -1670,7 +1670,7 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
new_values = self.values.shift(periods=periods, fill_value=fill_value)
return [self.make_block_same_class(new_values)]

def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
def where(self, other, cond, errors="raise") -> List[Block]:

cond = extract_bool_array(cond)
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
Expand Down Expand Up @@ -1837,7 +1837,7 @@ def putmask(self, mask, new) -> List[Block]:
arr.T.putmask(mask, new)
return [self]

def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
def where(self, other, cond, errors="raise") -> List[Block]:
# TODO(EA2D): reshape unnecessary with 2D EAs
arr = self.array_values().reshape(self.shape)

Expand All @@ -1846,7 +1846,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
try:
res_values = arr.T.where(cond, other).T
except (ValueError, TypeError):
return super().where(other, cond, errors=errors, axis=axis)
return super().where(other, cond, errors=errors)

# TODO(EA2D): reshape not needed with 2D EAs
res_values = res_values.reshape(self.values.shape)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ def quantile(

return type(self)(blocks, new_axes)

def where(self, other, cond, align: bool, errors: str, axis: int) -> BlockManager:
axis = self._normalize_axis(axis)
def where(self, other, cond, align: bool, errors: str) -> BlockManager:
if align:
align_keys = ["other", "cond"]
else:
Expand All @@ -588,7 +587,6 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> BlockManage
other=other,
cond=cond,
errors=errors,
axis=axis,
)

def setitem(self, indexer, value) -> BlockManager:
Expand Down