Skip to content

REF: share block methods #54058

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

Merged
merged 3 commits into from
Jul 10, 2023
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
49 changes: 28 additions & 21 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,34 @@ class Block(PandasObject):

__slots__ = ()
is_numeric = False
is_object = False
is_extension = False
_can_consolidate = True
_validate_ndim = True

@final
@cache_readonly
def _validate_ndim(self) -> bool:
"""
We validate dimension for blocks that can hold 2D values, which for now
means numpy dtypes or DatetimeTZDtype.
"""
dtype = self.dtype
return not isinstance(dtype, ExtensionDtype) or isinstance(
dtype, DatetimeTZDtype
)

@final
@cache_readonly
def is_object(self) -> bool:
return self.values.dtype == _dtype_obj

@final
@cache_readonly
def is_extension(self) -> bool:
return not lib.is_np_dtype(self.values.dtype)

@final
@cache_readonly
def _can_consolidate(self) -> bool:
# We _could_ consolidate for DatetimeTZDtype but don't for now.
return not self.is_extension

@final
@cache_readonly
Expand Down Expand Up @@ -1905,10 +1929,6 @@ class ExtensionBlock(libinternals.Block, EABackedBlock):
ExtensionArrays are limited to 1-D.
"""

_can_consolidate = False
_validate_ndim = False
is_extension = True

values: ExtensionArray

def fillna(
Expand Down Expand Up @@ -2172,10 +2192,6 @@ def is_numeric(self) -> bool: # type: ignore[override]

return kind in "fciub"

@cache_readonly
def is_object(self) -> bool: # type: ignore[override]
return self.values.dtype.kind == "O"


class NumericBlock(NumpyBlock):
# this Block type is kept for backwards-compatibility
Expand All @@ -2196,12 +2212,6 @@ class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock

values: NDArrayBackedExtensionArray

# error: Signature of "is_extension" incompatible with supertype "Block"
@cache_readonly
def is_extension(self) -> bool: # type: ignore[override]
# i.e. datetime64tz, PeriodDtype
return not isinstance(self.dtype, np.dtype)

@property
def is_view(self) -> bool:
"""return a boolean if I am possibly a view"""
Expand Down Expand Up @@ -2239,9 +2249,6 @@ class DatetimeTZBlock(DatetimeLikeBlock):
values: DatetimeArray

__slots__ = ()
is_extension = True
_validate_ndim = True
_can_consolidate = False

# Don't use values_for_json from DatetimeLikeBlock since it is
# an invalid optimization here(drop the tz)
Expand Down
39 changes: 0 additions & 39 deletions pandas/tests/extension/test_external_block.py

This file was deleted.