Skip to content

Commit 5f2cdf8

Browse files
authored
REF: call _block_shape from EABlock.make_block (#33308)
1 parent 5d0faa8 commit 5f2cdf8

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

pandas/core/internals/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
IntBlock,
1111
ObjectBlock,
1212
TimeDeltaBlock,
13-
_block_shape,
1413
_safe_reshape,
1514
make_block,
1615
)
@@ -31,7 +30,6 @@
3130
"TimeDeltaBlock",
3231
"_safe_reshape",
3332
"make_block",
34-
"_block_shape",
3533
"BlockManager",
3634
"SingleBlockManager",
3735
"concatenate_block_managers",

pandas/core/internals/blocks.py

+11-17
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ def make_block(self, values, placement=None) -> "Block":
243243
"""
244244
if placement is None:
245245
placement = self.mgr_locs
246+
if self.is_extension:
247+
values = _block_shape(values, ndim=self.ndim)
246248

247249
return make_block(values, placement=placement, ndim=self.ndim)
248250

@@ -354,13 +356,12 @@ def _split_op_result(self, result) -> List["Block"]:
354356
nbs = []
355357
for i, loc in enumerate(self.mgr_locs):
356358
vals = result[i]
357-
nv = _block_shape(vals, ndim=self.ndim)
358-
block = self.make_block(values=nv, placement=[loc])
359+
block = self.make_block(values=vals, placement=[loc])
359360
nbs.append(block)
360361
return nbs
361362

362363
if not isinstance(result, Block):
363-
result = self.make_block(values=_block_shape(result, ndim=self.ndim))
364+
result = self.make_block(result)
364365

365366
return [result]
366367

@@ -1264,9 +1265,6 @@ def take_nd(self, indexer, axis: int, new_mgr_locs=None, fill_value=lib.no_defau
12641265
def diff(self, n: int, axis: int = 1) -> List["Block"]:
12651266
""" return block for the diff of the values """
12661267
new_values = algos.diff(self.values, n, axis=axis, stacklevel=7)
1267-
# We use block_shape for ExtensionBlock subclasses, which may call here
1268-
# via a super.
1269-
new_values = _block_shape(new_values, ndim=self.ndim)
12701268
return [self.make_block(values=new_values)]
12711269

12721270
def shift(self, periods: int, axis: int = 0, fill_value=None):
@@ -2254,7 +2252,7 @@ def concat_same_type(self, to_concat):
22542252
values = values.astype(object, copy=False)
22552253
placement = self.mgr_locs if self.ndim == 2 else slice(len(values))
22562254

2257-
return self.make_block(_block_shape(values, self.ndim), placement=placement)
2255+
return self.make_block(values, placement=placement)
22582256
return super().concat_same_type(to_concat)
22592257

22602258
def fillna(self, value, limit=None, inplace=False, downcast=None):
@@ -2420,7 +2418,6 @@ def f(mask, val, idx):
24202418
# TODO: allow EA once reshape is supported
24212419
values = values.reshape(shape)
24222420

2423-
values = _block_shape(values, ndim=self.ndim)
24242421
return values
24252422

24262423
if self.ndim == 2:
@@ -2660,9 +2657,7 @@ def concat_same_type(self, to_concat):
26602657
)
26612658
placement = self.mgr_locs if self.ndim == 2 else slice(len(values))
26622659
# not using self.make_block_same_class as values can be object dtype
2663-
return self.make_block(
2664-
_block_shape(values, ndim=self.ndim), placement=placement
2665-
)
2660+
return self.make_block(values, placement=placement)
26662661

26672662
def replace(
26682663
self,
@@ -2771,16 +2766,15 @@ def _extend_blocks(result, blocks=None):
27712766
return blocks
27722767

27732768

2774-
def _block_shape(values, ndim=1, shape=None):
2769+
def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
27752770
""" guarantee the shape of the values to be at least 1 d """
27762771
if values.ndim < ndim:
2777-
if shape is None:
2778-
shape = values.shape
2779-
if not is_extension_array_dtype(values):
2780-
# TODO: https://github.com/pandas-dev/pandas/issues/23023
2772+
shape = values.shape
2773+
if not is_extension_array_dtype(values.dtype):
2774+
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
27812775
# block.shape is incorrect for "2D" ExtensionArrays
27822776
# We can't, and don't need to, reshape.
2783-
values = values.reshape(tuple((1,) + shape))
2777+
values = values.reshape(tuple((1,) + shape)) # type: ignore
27842778
return values
27852779

27862780

0 commit comments

Comments
 (0)