Skip to content

Commit 4554635

Browse files
authored
REF: remove DatetimeBlock, TimeDeltaBlock (#40614)
1 parent fb04a42 commit 4554635

File tree

9 files changed

+33
-56
lines changed

9 files changed

+33
-56
lines changed

pandas/core/internals/__init__.py

-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
)
1010
from pandas.core.internals.blocks import ( # io.pytables, io.packers
1111
Block,
12-
DatetimeBlock,
1312
DatetimeTZBlock,
1413
ExtensionBlock,
1514
NumericBlock,
1615
ObjectBlock,
17-
TimeDeltaBlock,
1816
)
1917
from pandas.core.internals.concat import concatenate_managers
2018
from pandas.core.internals.managers import (
@@ -28,11 +26,9 @@
2826
"Block",
2927
"CategoricalBlock",
3028
"NumericBlock",
31-
"DatetimeBlock",
3229
"DatetimeTZBlock",
3330
"ExtensionBlock",
3431
"ObjectBlock",
35-
"TimeDeltaBlock",
3632
"make_block",
3733
"DataManager",
3834
"ArrayManager",

pandas/core/internals/array_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def apply_with_block(self: T, f, align_keys=None, swap_axis=True, **kwargs) -> T
471471
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
472472
# attribute "tz"
473473
if hasattr(arr, "tz") and arr.tz is None: # type: ignore[union-attr]
474-
# DatetimeArray needs to be converted to ndarray for DatetimeBlock
474+
# DatetimeArray needs to be converted to ndarray for DatetimeLikeBlock
475475

476476
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
477477
# attribute "_data"

pandas/core/internals/blocks.py

+16-30
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
is_dtype_equal,
5151
is_extension_array_dtype,
5252
is_list_like,
53-
is_object_dtype,
5453
is_sparse,
5554
pandas_dtype,
5655
)
@@ -207,13 +206,6 @@ def is_bool(self) -> bool:
207206
def external_values(self):
208207
return external_values(self.values)
209208

210-
@final
211-
def internal_values(self):
212-
"""
213-
The array that Series._values returns (internal values).
214-
"""
215-
return self.values
216-
217209
@property
218210
def array_values(self) -> ExtensionArray:
219211
"""
@@ -1771,7 +1763,8 @@ def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
17711763
return object dtype as boxed values, such as Timestamps/Timedelta
17721764
"""
17731765
values = self.values
1774-
if is_object_dtype(dtype):
1766+
if dtype == _dtype_obj:
1767+
# DTA/TDA constructor and astype can handle 2D
17751768
values = values.astype(object)
17761769
# TODO(EA2D): reshape not needed with 2D EAs
17771770
return np.asarray(values).reshape(self.shape)
@@ -1821,7 +1814,7 @@ def diff(self, n: int, axis: int = 0) -> List[Block]:
18211814
18221815
Returns
18231816
-------
1824-
A list with a new TimeDeltaBlock.
1817+
A list with a new Block.
18251818
18261819
Notes
18271820
-----
@@ -1869,19 +1862,16 @@ def delete(self, loc) -> None:
18691862
pass
18701863

18711864

1872-
class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
1873-
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
1874-
1875-
values: Union[DatetimeArray, TimedeltaArray]
1865+
class DatetimeLikeBlock(NDArrayBackedExtensionBlock):
1866+
"""Mixin class for DatetimeLikeBlock, DatetimeTZBlock."""
18761867

1868+
__slots__ = ()
18771869
is_numeric = False
18781870

1879-
1880-
class DatetimeBlock(DatetimeLikeBlockMixin):
1881-
__slots__ = ()
1871+
values: Union[DatetimeArray, TimedeltaArray]
18821872

18831873

1884-
class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlockMixin):
1874+
class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlock):
18851875
""" implement a datetime64 block with a tz attribute """
18861876

18871877
values: DatetimeArray
@@ -1890,21 +1880,19 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlockMixin):
18901880
is_extension = True
18911881
is_numeric = False
18921882

1893-
diff = DatetimeBlock.diff
1894-
where = DatetimeBlock.where
1895-
putmask = DatetimeLikeBlockMixin.putmask
1896-
fillna = DatetimeLikeBlockMixin.fillna
1883+
diff = NDArrayBackedExtensionBlock.diff
1884+
where = NDArrayBackedExtensionBlock.where
1885+
putmask = NDArrayBackedExtensionBlock.putmask
1886+
fillna = NDArrayBackedExtensionBlock.fillna
1887+
1888+
get_values = NDArrayBackedExtensionBlock.get_values
18971889

18981890
# error: Incompatible types in assignment (expression has type
18991891
# "Callable[[NDArrayBackedExtensionBlock], bool]", base class "ExtensionBlock"
19001892
# defined the type as "bool") [assignment]
19011893
is_view = NDArrayBackedExtensionBlock.is_view # type: ignore[assignment]
19021894

19031895

1904-
class TimeDeltaBlock(DatetimeLikeBlockMixin):
1905-
__slots__ = ()
1906-
1907-
19081896
class ObjectBlock(Block):
19091897
__slots__ = ()
19101898
is_object = True
@@ -2022,10 +2010,8 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
20222010
# Note: need to be sure PandasArray is unwrapped before we get here
20232011
cls = ExtensionBlock
20242012

2025-
elif kind == "M":
2026-
cls = DatetimeBlock
2027-
elif kind == "m":
2028-
cls = TimeDeltaBlock
2013+
elif kind in ["M", "m"]:
2014+
cls = DatetimeLikeBlock
20292015
elif kind in ["f", "c", "i", "u", "b"]:
20302016
cls = NumericBlock
20312017
else:

pandas/core/internals/concat.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,16 @@ def _is_uniform_join_units(join_units: List[JoinUnit]) -> bool:
507507
_concatenate_join_units (which uses `concat_compat`).
508508
509509
"""
510-
# TODO: require dtype match in addition to same type? e.g. DatetimeTZBlock
511-
# cannot necessarily join
512510
return (
513511
# all blocks need to have the same type
514512
all(type(ju.block) is type(join_units[0].block) for ju in join_units) # noqa
515513
and
514+
# e.g. DatetimeLikeBlock can be dt64 or td64, but these are not uniform
515+
all(
516+
is_dtype_equal(ju.block.dtype, join_units[0].block.dtype)
517+
for ju in join_units
518+
)
519+
and
516520
# no blocks that would get missing values (can lead to type upcasts)
517521
# unless we're an extension dtype.
518522
all(not ju.is_na or ju.block.is_extension for ju in join_units)

pandas/core/internals/managers.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
from pandas.core.dtypes.cast import infer_dtype_from_scalar
3737
from pandas.core.dtypes.common import (
38-
DT64NS_DTYPE,
3938
ensure_int64,
4039
is_dtype_equal,
4140
is_extension_array_dtype,
@@ -1639,7 +1638,7 @@ def external_values(self):
16391638

16401639
def internal_values(self):
16411640
"""The array that Series._values returns"""
1642-
return self._block.internal_values()
1641+
return self._block.values
16431642

16441643
def array_values(self):
16451644
"""The array that Series.array returns"""
@@ -1794,17 +1793,11 @@ def _form_blocks(
17941793
)
17951794
blocks.extend(numeric_blocks)
17961795

1797-
if len(items_dict["TimeDeltaBlock"]):
1798-
timedelta_blocks = _multi_blockify(
1799-
items_dict["TimeDeltaBlock"], consolidate=consolidate
1796+
if len(items_dict["DatetimeLikeBlock"]):
1797+
dtlike_blocks = _multi_blockify(
1798+
items_dict["DatetimeLikeBlock"], consolidate=consolidate
18001799
)
1801-
blocks.extend(timedelta_blocks)
1802-
1803-
if len(items_dict["DatetimeBlock"]):
1804-
datetime_blocks = _simple_blockify(
1805-
items_dict["DatetimeBlock"], DT64NS_DTYPE, consolidate=consolidate
1806-
)
1807-
blocks.extend(datetime_blocks)
1800+
blocks.extend(dtlike_blocks)
18081801

18091802
if len(items_dict["DatetimeTZBlock"]):
18101803
dttz_blocks = [

pandas/tests/frame/methods/test_quantile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def test_quantile_box(self):
336336
)
337337
tm.assert_frame_equal(res, exp)
338338

339-
# DatetimeBlock may be consolidated and contain NaT in different loc
339+
# DatetimeLikeBlock may be consolidated and contain NaT in different loc
340340
df = DataFrame(
341341
{
342342
"A": [

pandas/tests/internals/test_api.py

-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ def test_namespace():
2727
expected = [
2828
"Block",
2929
"NumericBlock",
30-
"DatetimeBlock",
3130
"DatetimeTZBlock",
3231
"ExtensionBlock",
3332
"ObjectBlock",
34-
"TimeDeltaBlock",
3533
"make_block",
3634
"DataManager",
3735
"ArrayManager",

pandas/tests/series/methods/test_dropna.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_dropna_period_dtype(self):
7070
tm.assert_series_equal(result, expected)
7171

7272
def test_datetime64_tz_dropna(self):
73-
# DatetimeBlock
73+
# DatetimeLikeBlock
7474
ser = Series(
7575
[
7676
Timestamp("2011-01-01 10:00"),
@@ -85,7 +85,7 @@ def test_datetime64_tz_dropna(self):
8585
)
8686
tm.assert_series_equal(result, expected)
8787

88-
# DatetimeBlockTZ
88+
# DatetimeTZBlock
8989
idx = DatetimeIndex(
9090
["2011-01-01 10:00", NaT, "2011-01-03 10:00", NaT], tz="Asia/Tokyo"
9191
)

pandas/tests/series/methods/test_fillna.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def test_datetime64_fillna(self):
334334

335335
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
336336
def test_datetime64_tz_fillna(self, tz):
337-
# DatetimeBlock
337+
# DatetimeLikeBlock
338338
ser = Series(
339339
[
340340
Timestamp("2011-01-01 10:00"),
@@ -414,7 +414,7 @@ def test_datetime64_tz_fillna(self, tz):
414414
tm.assert_series_equal(expected, result)
415415
tm.assert_series_equal(isna(ser), null_loc)
416416

417-
# DatetimeBlockTZ
417+
# DatetimeTZBlock
418418
idx = DatetimeIndex(["2011-01-01 10:00", NaT, "2011-01-03 10:00", NaT], tz=tz)
419419
ser = Series(idx)
420420
assert ser.dtype == f"datetime64[ns, {tz}]"

0 commit comments

Comments
 (0)