Skip to content

Commit c03d930

Browse files
jbrockmendelJulianWgs
authored andcommitted
CLN: remove unused axis keyword from Block.where (pandas-dev#40561)
1 parent c427a5f commit c03d930

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

pandas/core/generic.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -8933,7 +8933,7 @@ def _where(
89338933
# align the cond to same shape as myself
89348934
cond = com.apply_if_callable(cond, self)
89358935
if isinstance(cond, NDFrame):
8936-
cond, _ = cond.align(self, join="right", broadcast_axis=1)
8936+
cond, _ = cond.align(self, join="right", broadcast_axis=1, copy=False)
89378937
else:
89388938
if not hasattr(cond, "shape"):
89398939
cond = np.asanyarray(cond)
@@ -8961,6 +8961,7 @@ def _where(
89618961
cond = cond.astype(bool)
89628962

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

89658966
# try to align with other
89668967
if isinstance(other, NDFrame):
@@ -8997,7 +8998,7 @@ def _where(
89978998
"cannot align with a higher dimensional NDFrame"
89988999
)
89999000

9000-
if not isinstance(other, (MultiIndex, NDFrame)):
9001+
elif not isinstance(other, (MultiIndex, NDFrame)):
90019002
# mainly just catching Index here
90029003
other = extract_array(other, extract_numpy=True)
90039004

@@ -9029,11 +9030,6 @@ def _where(
90299030
else:
90309031
align = self._get_axis_number(axis) == 1
90319032

9032-
if isinstance(cond, NDFrame):
9033-
cond = cond.reindex(
9034-
self._info_axis, axis=self._info_axis_number, copy=False
9035-
)
9036-
90379033
if inplace:
90389034
# we may have different type blocks come out of putmask, so
90399035
# reconstruct the block manager
@@ -9049,7 +9045,6 @@ def _where(
90499045
cond=cond,
90509046
align=align,
90519047
errors=errors,
9052-
axis=axis,
90539048
)
90549049
result = self._constructor(new_data)
90559050
return result.__finalize__(self)

pandas/core/internals/array_manager.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def quantile(
522522
axes = [qs, self._axes[1]]
523523
return type(self)(new_arrs, axes)
524524

525-
def where(self, other, cond, align: bool, errors: str, axis: int) -> ArrayManager:
525+
def where(self, other, cond, align: bool, errors: str) -> ArrayManager:
526526
if align:
527527
align_keys = ["other", "cond"]
528528
else:
@@ -535,7 +535,6 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> ArrayManage
535535
other=other,
536536
cond=cond,
537537
errors=errors,
538-
axis=axis,
539538
)
540539

541540
# TODO what is this used for?

pandas/core/internals/blocks.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
12911291

12921292
return [self.make_block(new_values)]
12931293

1294-
def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
1294+
def where(self, other, cond, errors="raise") -> List[Block]:
12951295
"""
12961296
evaluate the block; return result block(s) from the result
12971297
@@ -1302,14 +1302,14 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
13021302
errors : str, {'raise', 'ignore'}, default 'raise'
13031303
- ``raise`` : allow exceptions to be raised
13041304
- ``ignore`` : suppress exceptions. On error return original object
1305-
axis : int, default 0
13061305
13071306
Returns
13081307
-------
13091308
List[Block]
13101309
"""
13111310
import pandas.core.computation.expressions as expressions
13121311

1312+
assert cond.ndim == self.ndim
13131313
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
13141314

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

13231323
icond, noop = validate_putmask(values, ~cond)
13241324

1325-
if is_valid_na_for_dtype(other, self.dtype) and not self.is_object:
1325+
if is_valid_na_for_dtype(other, self.dtype) and self.dtype != _dtype_obj:
13261326
other = self.fill_value
13271327

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

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

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

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

16751675
cond = extract_bool_array(cond)
16761676
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
@@ -1828,7 +1828,7 @@ def putmask(self, mask, new) -> List[Block]:
18281828
arr.T.putmask(mask, new)
18291829
return [self]
18301830

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

@@ -1837,7 +1837,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
18371837
try:
18381838
res_values = arr.T.where(cond, other).T
18391839
except (ValueError, TypeError):
1840-
return super().where(other, cond, errors=errors, axis=axis)
1840+
return super().where(other, cond, errors=errors)
18411841

18421842
# TODO(EA2D): reshape not needed with 2D EAs
18431843
res_values = res_values.reshape(self.values.shape)

pandas/core/internals/managers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ def quantile(
574574

575575
return type(self)(blocks, new_axes)
576576

577-
def where(self, other, cond, align: bool, errors: str, axis: int) -> BlockManager:
578-
axis = self._normalize_axis(axis)
577+
def where(self, other, cond, align: bool, errors: str) -> BlockManager:
579578
if align:
580579
align_keys = ["other", "cond"]
581580
else:
@@ -588,7 +587,6 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> BlockManage
588587
other=other,
589588
cond=cond,
590589
errors=errors,
591-
axis=axis,
592590
)
593591

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

0 commit comments

Comments
 (0)