Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: de-duplicate Block.__init__ #38134

Merged
merged 16 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pandas/core/internals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import numpy as np

from pandas._libs.internals import BlockPlacement
from pandas._typing import Dtype

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

if not isinstance(placement, BlockPlacement):
placement = BlockPlacement(placement)

if ndim is None:
# GH#38134 Block constructor now assumes ndim is not None
if not isinstance(values.dtype, np.dtype):
if len(placement) != 1:
ndim = 1
else:
ndim = 2
else:
ndim = values.ndim

return klass(values, ndim=ndim, placement=placement)
38 changes: 7 additions & 31 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def __init__(self, values, placement, ndim: int):
f"placement implies {len(self.mgr_locs)}"
)

elif self.is_extension and self.ndim == 2 and len(self.mgr_locs) != 1:
# TODO(EA2D): check unnecessary with 2D EAs
raise AssertionError("block.size != values.size")

@classmethod
def _maybe_coerce_values(cls, values):
"""
Expand All @@ -185,7 +189,7 @@ def _maybe_coerce_values(cls, values):
"""
return values

def _check_ndim(self, values, ndim):
def _check_ndim(self, values, ndim: int):
"""
ndim inference and validation.

Expand All @@ -196,7 +200,7 @@ def _check_ndim(self, values, ndim):
Parameters
----------
values : array-like
ndim : int or None
ndim : int

Returns
-------
Expand All @@ -206,8 +210,7 @@ def _check_ndim(self, values, ndim):
------
ValueError : the number of dimensions do not match
"""
if ndim is None:
ndim = values.ndim
assert isinstance(ndim, int) # GH#38134 enforce this

if self._validate_ndim:
if values.ndim != ndim:
Expand Down Expand Up @@ -1481,33 +1484,6 @@ class ExtensionBlock(Block):

values: ExtensionArray

def __init__(self, values, placement, ndim: int):
"""
Initialize a non-consolidatable block.

'ndim' may be inferred from 'placement'.

This will call continue to call __init__ for the other base
classes mixed in with this Mixin.
"""

# Placement must be converted to BlockPlacement so that we can check
# its length
if not isinstance(placement, libinternals.BlockPlacement):
placement = libinternals.BlockPlacement(placement)

# Maybe infer ndim from placement
if ndim is None:
if len(placement) != 1:
ndim = 1
else:
ndim = 2
super().__init__(values, placement, ndim=ndim)

if self.ndim == 2 and len(self.mgr_locs) != 1:
# TODO(EA2D): check unnecessary with 2D EAs
raise AssertionError("block.size != values.size")

@property
def shape(self) -> Shape:
# TODO(EA2D): override unnecessary with 2D EAs
Expand Down