Skip to content

Commit 8cf0e3d

Browse files
Update managers.py
When a loc indexer goes to create a fresh array to hold values from an Extension Array, it makes the array allocation based on the na_value of the EA, but that na_value may be smaller than the size of the things that the EA can hold (such as complex128). Note that np.nan is itself a legitimate complex128 value. If the allocated array cannot hold the values from the block manager, and if the EA is not immutable, it will try a second strategy of allocating an array based on the dtype of the values of the blocks. If the blocks hold complex numbers, the array will be properly sized. This should close pandas-dev#54445. Signed-off-by: Michael Tiemann <[email protected]>
1 parent fc30823 commit 8cf0e3d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

pandas/core/internals/managers.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,18 @@ def fast_xs(self, loc: int) -> SingleBlockManager:
983983
)
984984
result = ensure_wrapped_if_datetimelike(result)
985985

986-
for blk in self.blocks:
987-
# Such assignment may incorrectly coerce NaT to None
988-
# result[blk.mgr_locs] = blk._slice((slice(None), loc))
989-
for i, rl in enumerate(blk.mgr_locs):
990-
result[rl] = blk.iget((i, loc))
986+
try:
987+
for blk in self.blocks:
988+
# Such assignment may incorrectly coerce NaT to None
989+
# result[blk.mgr_locs] = blk._slice((slice(None), loc))
990+
for i, rl in enumerate(blk.mgr_locs):
991+
result[rl] = blk.iget((i, loc))
992+
except TypeError:
993+
if isinstance(dtype, ExtensionDtype) and not immutable_ea:
994+
values = [v[loc] for v in self.arrays]
995+
result = cls._from_sequence(values, dtype)
996+
else:
997+
raise TypeError
991998

992999
if immutable_ea:
9931000
dtype = cast(ExtensionDtype, dtype)

0 commit comments

Comments
 (0)