Skip to content

Commit fea76c4

Browse files
authored
REF: de-duplicate block_shape vs safe_reshape (#39892)
1 parent 0b16108 commit fea76c4

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

pandas/core/internals/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
ObjectBlock,
1212
TimeDeltaBlock,
1313
make_block,
14-
safe_reshape,
1514
)
1615
from pandas.core.internals.concat import concatenate_block_managers
1716
from pandas.core.internals.managers import (
@@ -31,7 +30,6 @@
3130
"FloatBlock",
3231
"ObjectBlock",
3332
"TimeDeltaBlock",
34-
"safe_reshape",
3533
"make_block",
3634
"DataManager",
3735
"ArrayManager",

pandas/core/internals/blocks.py

+10-29
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def make_block(self, values, placement=None) -> Block:
308308
if placement is None:
309309
placement = self.mgr_locs
310310
if self.is_extension:
311-
values = _block_shape(values, ndim=self.ndim)
311+
values = ensure_block_shape(values, ndim=self.ndim)
312312

313313
return make_block(values, placement=placement, ndim=self.ndim)
314314

@@ -533,7 +533,7 @@ def make_a_block(nv, ref_loc):
533533
else:
534534
# Put back the dimension that was taken from it and make
535535
# a block out of the result.
536-
nv = _block_shape(nv, ndim=self.ndim)
536+
nv = ensure_block_shape(nv, ndim=self.ndim)
537537
block = self.make_block(values=nv, placement=ref_loc)
538538
return block
539539

@@ -1569,7 +1569,9 @@ def putmask(self, mask, new) -> List[Block]:
15691569
if isinstance(new, (np.ndarray, ExtensionArray)) and len(new) == len(mask):
15701570
new = new[mask]
15711571

1572-
mask = safe_reshape(mask, new_values.shape)
1572+
if mask.ndim == new_values.ndim + 1:
1573+
# TODO(EA2D): unnecessary with 2D EAs
1574+
mask = mask.reshape(new_values.shape)
15731575

15741576
new_values[mask] = new
15751577
return [self.make_block(values=new_values)]
@@ -2426,36 +2428,15 @@ def extend_blocks(result, blocks=None) -> List[Block]:
24262428
return blocks
24272429

24282430

2429-
def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
2430-
""" guarantee the shape of the values to be at least 1 d """
2431+
def ensure_block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
2432+
"""
2433+
Reshape if possible to have values.ndim == ndim.
2434+
"""
24312435
if values.ndim < ndim:
2432-
shape = values.shape
24332436
if not is_extension_array_dtype(values.dtype):
24342437
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
24352438
# block.shape is incorrect for "2D" ExtensionArrays
24362439
# We can't, and don't need to, reshape.
24372440

2438-
# error: "ExtensionArray" has no attribute "reshape"
2439-
values = values.reshape(tuple((1,) + shape)) # type: ignore[attr-defined]
2441+
values = np.asarray(values).reshape(1, -1)
24402442
return values
2441-
2442-
2443-
def safe_reshape(arr: ArrayLike, new_shape: Shape) -> ArrayLike:
2444-
"""
2445-
Reshape `arr` to have shape `new_shape`, unless it is an ExtensionArray,
2446-
in which case it will be returned unchanged (see gh-13012).
2447-
2448-
Parameters
2449-
----------
2450-
arr : np.ndarray or ExtensionArray
2451-
new_shape : Tuple[int]
2452-
2453-
Returns
2454-
-------
2455-
np.ndarray or ExtensionArray
2456-
"""
2457-
if not is_extension_array_dtype(arr.dtype):
2458-
# Note: this will include TimedeltaArray and tz-naive DatetimeArray
2459-
# TODO(EA2D): special case will be unnecessary with 2D EAs
2460-
arr = np.asarray(arr).reshape(new_shape)
2461-
return arr

pandas/core/internals/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@
6969
DatetimeTZBlock,
7070
ExtensionBlock,
7171
ObjectValuesExtensionBlock,
72+
ensure_block_shape,
7273
extend_blocks,
7374
get_block_type,
7475
make_block,
75-
safe_reshape,
7676
)
7777
from pandas.core.internals.ops import (
7878
blockwise_all,
@@ -1042,7 +1042,7 @@ def value_getitem(placement):
10421042
value = value.T
10431043

10441044
if value.ndim == self.ndim - 1:
1045-
value = safe_reshape(value, (1,) + value.shape)
1045+
value = ensure_block_shape(value, ndim=2)
10461046

10471047
def value_getitem(placement):
10481048
return value
@@ -1167,7 +1167,7 @@ def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False
11671167
value = value.T
11681168
elif value.ndim == self.ndim - 1 and not is_extension_array_dtype(value.dtype):
11691169
# TODO(EA2D): special case not needed with 2D EAs
1170-
value = safe_reshape(value, (1,) + value.shape)
1170+
value = ensure_block_shape(value, ndim=2)
11711171

11721172
block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
11731173

0 commit comments

Comments
 (0)