diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 51ca9dbd763b4..2fd0cc79a8ca8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 + result = self._constructor_sliced(new_mgr, name=self.index[i]).__finalize__( + self + ) result._set_is_copy(self, copy=copy) return result diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 24c3bcb7bf669..ad70053dd6d2c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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)] diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 7c877af0585e7..0c16ddbbc21ab 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -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]`. @@ -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) @@ -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: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 2e638f5b0fb3d..f530c58bbd0c0 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -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]`. @@ -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]) @@ -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: """ diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index c86150d10fde4..46bb7b3bfddab 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -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)