Skip to content

Commit d5ba8c0

Browse files
REF: Manager.fast_xs to return SingleBlockManager instead of array (#47077)
1 parent e039117 commit d5ba8c0

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

pandas/core/frame.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -3484,16 +3484,13 @@ def _ixs(self, i: int, axis: int = 0):
34843484
"""
34853485
# irow
34863486
if axis == 0:
3487-
new_values = self._mgr.fast_xs(i)
3487+
new_mgr = self._mgr.fast_xs(i)
34883488

34893489
# if we are a copy, mark as such
3490-
copy = isinstance(new_values, np.ndarray) and new_values.base is None
3491-
result = self._constructor_sliced(
3492-
new_values,
3493-
index=self.columns,
3494-
name=self.index[i],
3495-
dtype=new_values.dtype,
3496-
).__finalize__(self)
3490+
copy = isinstance(new_mgr.array, np.ndarray) and new_mgr.array.base is None
3491+
result = self._constructor_sliced(new_mgr, name=self.index[i]).__finalize__(
3492+
self
3493+
)
34973494
result._set_is_copy(self, copy=copy)
34983495
return result
34993496

pandas/core/generic.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3815,13 +3815,10 @@ class animal locomotion
38153815
# so just return them (GH 6394)
38163816
return self._values[loc]
38173817

3818-
new_values = self._mgr.fast_xs(loc)
3818+
new_mgr = self._mgr.fast_xs(loc)
38193819

38203820
result = self._constructor_sliced(
3821-
new_values,
3822-
index=self.columns,
3823-
name=self.index[loc],
3824-
dtype=new_values.dtype,
3821+
new_mgr, name=self.index[loc]
38253822
).__finalize__(self)
38263823
elif is_scalar(loc):
38273824
result = self.iloc[:, slice(loc, loc + 1)]

pandas/core/internals/array_manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def _verify_integrity(self) -> None:
749749
# --------------------------------------------------------------------
750750
# Indexing
751751

752-
def fast_xs(self, loc: int) -> ArrayLike:
752+
def fast_xs(self, loc: int) -> SingleArrayManager:
753753
"""
754754
Return the array corresponding to `frame.iloc[loc]`.
755755
@@ -773,7 +773,7 @@ def fast_xs(self, loc: int) -> ArrayLike:
773773
result = TimedeltaArray._from_sequence(values, dtype=dtype)._data
774774
else:
775775
result = np.array(values, dtype=dtype)
776-
return result
776+
return SingleArrayManager([result], [self._axes[1]])
777777

778778
def get_slice(self, slobj: slice, axis: int = 0) -> ArrayManager:
779779
axis = self._normalize_axis(axis)
@@ -1261,7 +1261,7 @@ def _can_hold_na(self) -> bool:
12611261
def is_single_block(self) -> bool:
12621262
return True
12631263

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

12671267
def get_slice(self, slobj: slice, axis: int = 0) -> SingleArrayManager:

pandas/core/internals/managers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ def from_blocks(cls, blocks: list[Block], axes: list[Index]) -> BlockManager:
945945
# ----------------------------------------------------------------
946946
# Indexing
947947

948-
def fast_xs(self, loc: int) -> ArrayLike:
948+
def fast_xs(self, loc: int) -> SingleBlockManager:
949949
"""
950950
Return the array corresponding to `frame.iloc[loc]`.
951951
@@ -958,7 +958,9 @@ def fast_xs(self, loc: int) -> ArrayLike:
958958
np.ndarray or ExtensionArray
959959
"""
960960
if len(self.blocks) == 1:
961-
return self.blocks[0].iget((slice(None), loc))
961+
result = self.blocks[0].iget((slice(None), loc))
962+
block = new_block(result, placement=slice(0, len(result)), ndim=1)
963+
return SingleBlockManager(block, self.axes[0])
962964

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

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

979-
return result
981+
block = new_block(result, placement=slice(0, len(result)), ndim=1)
982+
return SingleBlockManager(block, self.axes[0])
980983

981984
def iget(self, i: int) -> SingleBlockManager:
982985
"""

pandas/tests/frame/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ def test_object_casting_indexing_wraps_datetimelike(using_array_manager):
13551355

13561356
mgr = df._mgr
13571357
mgr._rebuild_blknos_and_blklocs()
1358-
arr = mgr.fast_xs(0)
1358+
arr = mgr.fast_xs(0).array
13591359
assert isinstance(arr[1], Timestamp)
13601360
assert isinstance(arr[2], pd.Timedelta)
13611361

0 commit comments

Comments
 (0)