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: Manager.fast_xs to return SingleBlockManager instead of array #47077

Merged
merged 1 commit into from
May 22, 2022
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
13 changes: 5 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3484,16 +3484,13 @@ def _ixs(self, i: int, axis: int = 0):
"""
# irow
if axis == 0:
new_values = self._mgr.fast_xs(i)
new_mgr = self._mgr.fast_xs(i)

# if we are a copy, mark as such
copy = isinstance(new_values, np.ndarray) and new_values.base is None
result = self._constructor_sliced(
new_values,
index=self.columns,
name=self.index[i],
dtype=new_values.dtype,
).__finalize__(self)
copy = isinstance(new_mgr.array, np.ndarray) and new_mgr.array.base is None
Copy link
Member

Choose a reason for hiding this comment

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

not for this PR, but this line is something we should handle more systematically, will be wrong for NDArrayBackedEAs

result = self._constructor_sliced(new_mgr, name=self.index[i]).__finalize__(
self
)
result._set_is_copy(self, copy=copy)
return result

Expand Down
7 changes: 2 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3815,13 +3815,10 @@ class animal locomotion
# so just return them (GH 6394)
return self._values[loc]

new_values = self._mgr.fast_xs(loc)
new_mgr = self._mgr.fast_xs(loc)

result = self._constructor_sliced(
new_values,
index=self.columns,
name=self.index[loc],
dtype=new_values.dtype,
new_mgr, name=self.index[loc]
).__finalize__(self)
elif is_scalar(loc):
result = self.iloc[:, slice(loc, loc + 1)]
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def _verify_integrity(self) -> None:
# --------------------------------------------------------------------
# Indexing

def fast_xs(self, loc: int) -> ArrayLike:
def fast_xs(self, loc: int) -> SingleArrayManager:
"""
Return the array corresponding to `frame.iloc[loc]`.

Expand All @@ -773,7 +773,7 @@ def fast_xs(self, loc: int) -> ArrayLike:
result = TimedeltaArray._from_sequence(values, dtype=dtype)._data
else:
result = np.array(values, dtype=dtype)
return result
return SingleArrayManager([result], [self._axes[1]])

def get_slice(self, slobj: slice, axis: int = 0) -> ArrayManager:
axis = self._normalize_axis(axis)
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def _can_hold_na(self) -> bool:
def is_single_block(self) -> bool:
return True

def fast_xs(self, loc: int) -> ArrayLike:
def fast_xs(self, loc: int) -> SingleArrayManager:
raise NotImplementedError("Use series._values[loc] instead")

def get_slice(self, slobj: slice, axis: int = 0) -> SingleArrayManager:
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def from_blocks(cls, blocks: list[Block], axes: list[Index]) -> BlockManager:
# ----------------------------------------------------------------
# Indexing

def fast_xs(self, loc: int) -> ArrayLike:
def fast_xs(self, loc: int) -> SingleBlockManager:
"""
Return the array corresponding to `frame.iloc[loc]`.

Expand All @@ -958,7 +958,9 @@ def fast_xs(self, loc: int) -> ArrayLike:
np.ndarray or ExtensionArray
"""
if len(self.blocks) == 1:
return self.blocks[0].iget((slice(None), loc))
result = self.blocks[0].iget((slice(None), loc))
block = new_block(result, placement=slice(0, len(result)), ndim=1)
return SingleBlockManager(block, self.axes[0])

dtype = interleaved_dtype([blk.dtype for blk in self.blocks])

Expand All @@ -976,7 +978,8 @@ def fast_xs(self, loc: int) -> ArrayLike:
for i, rl in enumerate(blk.mgr_locs):
result[rl] = blk.iget((i, loc))

return result
block = new_block(result, placement=slice(0, len(result)), ndim=1)
return SingleBlockManager(block, self.axes[0])

def iget(self, i: int) -> SingleBlockManager:
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ def test_object_casting_indexing_wraps_datetimelike(using_array_manager):

mgr = df._mgr
mgr._rebuild_blknos_and_blklocs()
arr = mgr.fast_xs(0)
arr = mgr.fast_xs(0).array
assert isinstance(arr[1], Timestamp)
assert isinstance(arr[2], pd.Timedelta)

Expand Down