Skip to content

Commit 2d0dbf3

Browse files
authored
REF: de-duplicate Block.__init__ (#38134)
1 parent 00e684f commit 2d0dbf3

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

pandas/core/internals/api.py

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import numpy as np
1212

13+
from pandas._libs.internals import BlockPlacement
1314
from pandas._typing import Dtype
1415

1516
from pandas.core.dtypes.common import is_datetime64tz_dtype
@@ -58,4 +59,17 @@ def make_block(
5859
# for e.g. pyarrow?
5960
values = DatetimeArray._simple_new(values, dtype=dtype)
6061

62+
if not isinstance(placement, BlockPlacement):
63+
placement = BlockPlacement(placement)
64+
65+
if ndim is None:
66+
# GH#38134 Block constructor now assumes ndim is not None
67+
if not isinstance(values.dtype, np.dtype):
68+
if len(placement) != 1:
69+
ndim = 1
70+
else:
71+
ndim = 2
72+
else:
73+
ndim = values.ndim
74+
6175
return klass(values, ndim=ndim, placement=placement)

pandas/core/internals/blocks.py

+7-31
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ def __init__(self, values, placement, ndim: int):
170170
f"placement implies {len(self.mgr_locs)}"
171171
)
172172

173+
elif self.is_extension and self.ndim == 2 and len(self.mgr_locs) != 1:
174+
# TODO(EA2D): check unnecessary with 2D EAs
175+
raise AssertionError("block.size != values.size")
176+
173177
@classmethod
174178
def _maybe_coerce_values(cls, values):
175179
"""
@@ -185,7 +189,7 @@ def _maybe_coerce_values(cls, values):
185189
"""
186190
return values
187191

188-
def _check_ndim(self, values, ndim):
192+
def _check_ndim(self, values, ndim: int):
189193
"""
190194
ndim inference and validation.
191195
@@ -196,7 +200,7 @@ def _check_ndim(self, values, ndim):
196200
Parameters
197201
----------
198202
values : array-like
199-
ndim : int or None
203+
ndim : int
200204
201205
Returns
202206
-------
@@ -206,8 +210,7 @@ def _check_ndim(self, values, ndim):
206210
------
207211
ValueError : the number of dimensions do not match
208212
"""
209-
if ndim is None:
210-
ndim = values.ndim
213+
assert isinstance(ndim, int) # GH#38134 enforce this
211214

212215
if self._validate_ndim:
213216
if values.ndim != ndim:
@@ -1466,33 +1469,6 @@ class ExtensionBlock(Block):
14661469

14671470
values: ExtensionArray
14681471

1469-
def __init__(self, values, placement, ndim: int):
1470-
"""
1471-
Initialize a non-consolidatable block.
1472-
1473-
'ndim' may be inferred from 'placement'.
1474-
1475-
This will call continue to call __init__ for the other base
1476-
classes mixed in with this Mixin.
1477-
"""
1478-
1479-
# Placement must be converted to BlockPlacement so that we can check
1480-
# its length
1481-
if not isinstance(placement, libinternals.BlockPlacement):
1482-
placement = libinternals.BlockPlacement(placement)
1483-
1484-
# Maybe infer ndim from placement
1485-
if ndim is None:
1486-
if len(placement) != 1:
1487-
ndim = 1
1488-
else:
1489-
ndim = 2
1490-
super().__init__(values, placement, ndim=ndim)
1491-
1492-
if self.ndim == 2 and len(self.mgr_locs) != 1:
1493-
# TODO(EA2D): check unnecessary with 2D EAs
1494-
raise AssertionError("block.size != values.size")
1495-
14961472
@property
14971473
def shape(self) -> Shape:
14981474
# TODO(EA2D): override unnecessary with 2D EAs

0 commit comments

Comments
 (0)