Skip to content
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

REF: get .items out of BlockManager.apply #33616

Merged
merged 1 commit into from
Apr 19, 2020
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
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 7 additions & 9 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't like the

indexer = tupel([None] * obj.ndim)
indexer[-1] = b.mgr_locs.indexer
kwargs[k] = obj.iloc[indexer]._values

?

Copy link
Member Author

Choose a reason for hiding this comment

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

actually i have a note to myself to make iloc handle Ellipsis so this can become obj.iloc[..., indexer]._values

xref #10956

Copy link
Contributor

Choose a reason for hiding this comment

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

ok great

else:
kwargs[k] = obj.iloc[:, b.mgr_locs.indexer]._values
else:
# otherwise we have an ndarray
kwargs[k] = obj[b.mgr_locs.indexer]
Expand Down Expand Up @@ -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))
Expand Down