From feb1703fd17e7dfda50b01d337c4b0abd62e4ab8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 17 Apr 2020 18:05:20 -0700 Subject: [PATCH] REF: get .items out of BlockManager.apply --- pandas/core/generic.py | 5 +++++ pandas/core/internals/managers.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a28f89a79a880..ac0dffa8f6aa8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8665,6 +8665,11 @@ def _where( else: align = self._get_axis_number(axis) == 1 + if align and isinstance(other, NDFrame): + other = other.reindex(self._info_axis, axis=self._info_axis_number) + if isinstance(cond, NDFrame): + cond = cond.reindex(self._info_axis, axis=self._info_axis_number) + block_axis = self._get_block_manager_axis(axis) if inplace: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index dd950c0276646..6368a2498b04c 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -373,23 +373,20 @@ def apply(self: T, f, align_keys=None, **kwargs) -> T: self._consolidate_inplace() - align_copy = False - if f == "where": - align_copy = True - aligned_args = {k: kwargs[k] for k in align_keys} for b in self.blocks: if aligned_args: - b_items = self.items[b.mgr_locs.indexer] for k, obj in aligned_args.items(): if isinstance(obj, (ABCSeries, ABCDataFrame)): - axis = obj._info_axis_number - kwargs[k] = obj.reindex( - b_items, axis=axis, copy=align_copy - )._values + # The caller is responsible for ensuring that + # obj.axes[-1].equals(self.items) + if obj.ndim == 1: + kwargs[k] = obj.iloc[b.mgr_locs.indexer]._values + else: + kwargs[k] = obj.iloc[:, b.mgr_locs.indexer]._values else: # otherwise we have an ndarray kwargs[k] = obj[b.mgr_locs.indexer] @@ -1125,6 +1122,7 @@ def insert(self, loc: int, item: Label, value, allow_duplicates: bool = False): new_axis = self.items.insert(loc, item) if value.ndim == self.ndim - 1 and not is_extension_array_dtype(value.dtype): + # TODO(EA2D): special case not needed with 2D EAs value = _safe_reshape(value, (1,) + value.shape) block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))