@@ -308,7 +308,7 @@ def make_block(self, values, placement=None) -> Block:
308
308
if placement is None :
309
309
placement = self .mgr_locs
310
310
if self .is_extension :
311
- values = _block_shape (values , ndim = self .ndim )
311
+ values = ensure_block_shape (values , ndim = self .ndim )
312
312
313
313
return make_block (values , placement = placement , ndim = self .ndim )
314
314
@@ -533,7 +533,7 @@ def make_a_block(nv, ref_loc):
533
533
else :
534
534
# Put back the dimension that was taken from it and make
535
535
# a block out of the result.
536
- nv = _block_shape (nv , ndim = self .ndim )
536
+ nv = ensure_block_shape (nv , ndim = self .ndim )
537
537
block = self .make_block (values = nv , placement = ref_loc )
538
538
return block
539
539
@@ -1569,7 +1569,9 @@ def putmask(self, mask, new) -> List[Block]:
1569
1569
if isinstance (new , (np .ndarray , ExtensionArray )) and len (new ) == len (mask ):
1570
1570
new = new [mask ]
1571
1571
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 )
1573
1575
1574
1576
new_values [mask ] = new
1575
1577
return [self .make_block (values = new_values )]
@@ -2426,36 +2428,15 @@ def extend_blocks(result, blocks=None) -> List[Block]:
2426
2428
return blocks
2427
2429
2428
2430
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
+ """
2431
2435
if values .ndim < ndim :
2432
- shape = values .shape
2433
2436
if not is_extension_array_dtype (values .dtype ):
2434
2437
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
2435
2438
# block.shape is incorrect for "2D" ExtensionArrays
2436
2439
# We can't, and don't need to, reshape.
2437
2440
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 )
2440
2442
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
0 commit comments