MCPcopy
hub / github.com/pandas-dev/pandas / fast_xs

Method fast_xs

pandas/core/internals/managers.py:1153–1224  ·  view source on GitHub ↗

Return the array corresponding to `frame.iloc[loc]`. Parameters ---------- loc : int Returns ------- np.ndarray or ExtensionArray

(self, loc: int)

Source from the content-addressed store, hash-verified

1151 # Indexing
1152
1153 def fast_xs(self, loc: int) -> SingleBlockManager:
1154 """
1155 Return the array corresponding to `frame.iloc[loc]`.
1156
1157 Parameters
1158 ----------
1159 loc : int
1160
1161 Returns
1162 -------
1163 np.ndarray or ExtensionArray
1164 """
1165 if len(self.blocks) == 1:
1166 # TODO: this could be wrong if blk.mgr_locs is not slice(None)-like;
1167 # is this ruled out in the general case?
1168 result: np.ndarray | ExtensionArray = self.blocks[0].iget(
1169 (slice(None), loc)
1170 )
1171 # in the case of a single block, the new block is a view
1172 bp = BlockPlacement(slice(0, len(result)))
1173 block = new_block(
1174 result,
1175 placement=bp,
1176 ndim=1,
1177 refs=self.blocks[0].refs,
1178 )
1179 return SingleBlockManager(block, self.axes[0].view())
1180
1181 dtype = interleaved_dtype([blk.dtype for blk in self.blocks])
1182
1183 n = len(self)
1184
1185 if isinstance(dtype, ExtensionDtype):
1186 # TODO: use object dtype as workaround for non-performant
1187 # EA.__setitem__ methods. (primarily ArrowExtensionArray.__setitem__
1188 # when iteratively setting individual values)
1189 # https://github.com/pandas-dev/pandas/pull/54508#issuecomment-1675827918
1190 result = np.empty(n, dtype=object)
1191 else:
1192 result = np.empty(n, dtype=dtype)
1193 result = ensure_wrapped_if_datetimelike(result)
1194
1195 for blk in self.blocks:
1196 # Such assignment may incorrectly coerce NaT to None
1197 # result[blk.mgr_locs] = blk._slice((slice(None), loc))
1198 for i, rl in enumerate(blk.mgr_locs):
1199 item = blk.iget((i, loc))
1200 if (
1201 result.dtype.kind in "iub"
1202 and lib.is_float(item)
1203 and isna(item)
1204 and isinstance(blk.dtype, CategoricalDtype)
1205 ):
1206 # GH#58954 caused bc interleaved_dtype is wrong for Categorical
1207 # TODO(GH#38240) this will be unnecessary
1208 # Note that doing this in a try/except would work for the
1209 # integer case, but not for bool, which will cast the NaN
1210 # entry to True.

Callers 3

_ixsMethod · 0.45
xsMethod · 0.45

Calls 11

new_blockFunction · 0.90
isnaFunction · 0.90
SingleBlockManagerClass · 0.85
interleaved_dtypeFunction · 0.85
igetMethod · 0.45
viewMethod · 0.45
emptyMethod · 0.45
astypeMethod · 0.45
construct_array_typeMethod · 0.45
_from_sequenceMethod · 0.45