Skip to content

Commit 48690d4

Browse files
authored
REF: share block methods (#54058)
* REF: share block methods * Remove unnecessary test
1 parent f725c1e commit 48690d4

File tree

2 files changed

+28
-60
lines changed

2 files changed

+28
-60
lines changed

pandas/core/internals/blocks.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,34 @@ class Block(PandasObject):
159159

160160
__slots__ = ()
161161
is_numeric = False
162-
is_object = False
163-
is_extension = False
164-
_can_consolidate = True
165-
_validate_ndim = True
162+
163+
@final
164+
@cache_readonly
165+
def _validate_ndim(self) -> bool:
166+
"""
167+
We validate dimension for blocks that can hold 2D values, which for now
168+
means numpy dtypes or DatetimeTZDtype.
169+
"""
170+
dtype = self.dtype
171+
return not isinstance(dtype, ExtensionDtype) or isinstance(
172+
dtype, DatetimeTZDtype
173+
)
174+
175+
@final
176+
@cache_readonly
177+
def is_object(self) -> bool:
178+
return self.values.dtype == _dtype_obj
179+
180+
@final
181+
@cache_readonly
182+
def is_extension(self) -> bool:
183+
return not lib.is_np_dtype(self.values.dtype)
184+
185+
@final
186+
@cache_readonly
187+
def _can_consolidate(self) -> bool:
188+
# We _could_ consolidate for DatetimeTZDtype but don't for now.
189+
return not self.is_extension
166190

167191
@final
168192
@cache_readonly
@@ -1908,10 +1932,6 @@ class ExtensionBlock(libinternals.Block, EABackedBlock):
19081932
ExtensionArrays are limited to 1-D.
19091933
"""
19101934

1911-
_can_consolidate = False
1912-
_validate_ndim = False
1913-
is_extension = True
1914-
19151935
values: ExtensionArray
19161936

19171937
def fillna(
@@ -2175,10 +2195,6 @@ def is_numeric(self) -> bool: # type: ignore[override]
21752195

21762196
return kind in "fciub"
21772197

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

21832199
class NumericBlock(NumpyBlock):
21842200
# this Block type is kept for backwards-compatibility
@@ -2199,12 +2215,6 @@ class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock
21992215

22002216
values: NDArrayBackedExtensionArray
22012217

2202-
# error: Signature of "is_extension" incompatible with supertype "Block"
2203-
@cache_readonly
2204-
def is_extension(self) -> bool: # type: ignore[override]
2205-
# i.e. datetime64tz, PeriodDtype
2206-
return not isinstance(self.dtype, np.dtype)
2207-
22082218
@property
22092219
def is_view(self) -> bool:
22102220
"""return a boolean if I am possibly a view"""
@@ -2242,9 +2252,6 @@ class DatetimeTZBlock(DatetimeLikeBlock):
22422252
values: DatetimeArray
22432253

22442254
__slots__ = ()
2245-
is_extension = True
2246-
_validate_ndim = True
2247-
_can_consolidate = False
22482255

22492256
# Don't use values_for_json from DatetimeLikeBlock since it is
22502257
# an invalid optimization here(drop the tz)

pandas/tests/extension/test_external_block.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)