Skip to content

Commit 5bd8d62

Browse files
authored
REF: roll IntBlock, BoolBlock, ComplexBlock into NumericBlock (#39236)
1 parent d512b31 commit 5bd8d62

File tree

7 files changed

+27
-56
lines changed

7 files changed

+27
-56
lines changed

pandas/core/internals/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
from pandas.core.internals.base import DataManager
33
from pandas.core.internals.blocks import ( # io.pytables, io.packers
44
Block,
5-
BoolBlock,
65
CategoricalBlock,
7-
ComplexBlock,
86
DatetimeBlock,
97
DatetimeTZBlock,
108
ExtensionBlock,
119
FloatBlock,
12-
IntBlock,
10+
NumericBlock,
1311
ObjectBlock,
1412
TimeDeltaBlock,
1513
make_block,
@@ -25,14 +23,12 @@
2523

2624
__all__ = [
2725
"Block",
28-
"BoolBlock",
2926
"CategoricalBlock",
30-
"ComplexBlock",
27+
"NumericBlock",
3128
"DatetimeBlock",
3229
"DatetimeTZBlock",
3330
"ExtensionBlock",
3431
"FloatBlock",
35-
"IntBlock",
3632
"ObjectBlock",
3733
"TimeDeltaBlock",
3834
"safe_reshape",

pandas/core/internals/blocks.py

+14-30
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ class Block(PandasObject):
100100
__slots__ = ["_mgr_locs", "values", "ndim"]
101101
is_numeric = False
102102
is_float = False
103-
is_integer = False
104-
is_complex = False
105103
is_datetime = False
106104
is_datetimetz = False
107105
is_timedelta = False
@@ -1195,7 +1193,7 @@ def _interpolate(
11951193

11961194
# only deal with floats
11971195
if not self.is_float:
1198-
if not self.is_integer:
1196+
if self.dtype.kind not in ["i", "u"]:
11991197
return [self]
12001198
data = data.astype(np.float64)
12011199

@@ -1316,7 +1314,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List["Block"]:
13161314
# see if we can operate on the entire block, or need item-by-item
13171315
# or if we are a single block (ndim == 1)
13181316
if (
1319-
(self.is_integer or self.is_bool)
1317+
(self.dtype.kind in ["b", "i", "u"])
13201318
and lib.is_float(other)
13211319
and np.isnan(other)
13221320
):
@@ -1332,7 +1330,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List["Block"]:
13321330
return self._maybe_downcast(blocks, "infer")
13331331

13341332
if not (
1335-
(self.is_integer or self.is_bool)
1333+
(self.dtype.kind in ["b", "i", "u"])
13361334
and lib.is_float(other)
13371335
and np.isnan(other)
13381336
):
@@ -1912,11 +1910,18 @@ def external_values(self):
19121910
class NumericBlock(Block):
19131911
__slots__ = ()
19141912
is_numeric = True
1915-
_can_hold_na = True
19161913

19171914
def _can_hold_element(self, element: Any) -> bool:
19181915
return can_hold_element(self.dtype, element)
19191916

1917+
@property
1918+
def _can_hold_na(self):
1919+
return self.dtype.kind not in ["b", "i", "u"]
1920+
1921+
@property
1922+
def is_bool(self):
1923+
return self.dtype.kind == "b"
1924+
19201925

19211926
class FloatBlock(NumericBlock):
19221927
__slots__ = ()
@@ -1956,17 +1961,6 @@ def to_native_types(
19561961
return self.make_block(res)
19571962

19581963

1959-
class ComplexBlock(NumericBlock):
1960-
__slots__ = ()
1961-
is_complex = True
1962-
1963-
1964-
class IntBlock(NumericBlock):
1965-
__slots__ = ()
1966-
is_integer = True
1967-
_can_hold_na = False
1968-
1969-
19701964
class DatetimeLikeBlockMixin(HybridMixin, Block):
19711965
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
19721966

@@ -2232,7 +2226,7 @@ def _check_ndim(self, values, ndim):
22322226
return ndim
22332227

22342228

2235-
class TimeDeltaBlock(DatetimeLikeBlockMixin, IntBlock):
2229+
class TimeDeltaBlock(DatetimeLikeBlockMixin):
22362230
__slots__ = ()
22372231
is_timedelta = True
22382232
_can_hold_na = True
@@ -2269,12 +2263,6 @@ def fillna(self, value, **kwargs):
22692263
return super().fillna(value, **kwargs)
22702264

22712265

2272-
class BoolBlock(NumericBlock):
2273-
__slots__ = ()
2274-
is_bool = True
2275-
_can_hold_na = False
2276-
2277-
22782266
class ObjectBlock(Block):
22792267
__slots__ = ()
22802268
is_object = True
@@ -2477,12 +2465,8 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
24772465
cls = TimeDeltaBlock
24782466
elif kind == "f":
24792467
cls = FloatBlock
2480-
elif kind == "c":
2481-
cls = ComplexBlock
2482-
elif kind == "i" or kind == "u":
2483-
cls = IntBlock
2484-
elif kind == "b":
2485-
cls = BoolBlock
2468+
elif kind in ["c", "i", "u", "b"]:
2469+
cls = NumericBlock
24862470
else:
24872471
cls = ObjectBlock
24882472
return cls

pandas/core/internals/managers.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -1732,18 +1732,14 @@ def _form_blocks(arrays, names: Index, axes) -> List[Block]:
17321732
float_blocks = _multi_blockify(items_dict["FloatBlock"])
17331733
blocks.extend(float_blocks)
17341734

1735-
if len(items_dict["ComplexBlock"]):
1736-
complex_blocks = _multi_blockify(items_dict["ComplexBlock"])
1735+
if len(items_dict["NumericBlock"]):
1736+
complex_blocks = _multi_blockify(items_dict["NumericBlock"])
17371737
blocks.extend(complex_blocks)
17381738

17391739
if len(items_dict["TimeDeltaBlock"]):
17401740
timedelta_blocks = _multi_blockify(items_dict["TimeDeltaBlock"])
17411741
blocks.extend(timedelta_blocks)
17421742

1743-
if len(items_dict["IntBlock"]):
1744-
int_blocks = _multi_blockify(items_dict["IntBlock"])
1745-
blocks.extend(int_blocks)
1746-
17471743
if len(items_dict["DatetimeBlock"]):
17481744
datetime_blocks = _simple_blockify(items_dict["DatetimeBlock"], DT64NS_DTYPE)
17491745
blocks.extend(datetime_blocks)
@@ -1755,10 +1751,6 @@ def _form_blocks(arrays, names: Index, axes) -> List[Block]:
17551751
]
17561752
blocks.extend(dttz_blocks)
17571753

1758-
if len(items_dict["BoolBlock"]):
1759-
bool_blocks = _simple_blockify(items_dict["BoolBlock"], np.bool_)
1760-
blocks.extend(bool_blocks)
1761-
17621754
if len(items_dict["ObjectBlock"]) > 0:
17631755
object_blocks = _simple_blockify(items_dict["ObjectBlock"], np.object_)
17641756
blocks.extend(object_blocks)

pandas/tests/frame/test_block_internals.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
option_context,
1919
)
2020
import pandas._testing as tm
21-
from pandas.core.internals import ObjectBlock
22-
from pandas.core.internals.blocks import IntBlock
21+
from pandas.core.internals import NumericBlock, ObjectBlock
2322

2423
# Segregated collection of methods that require the BlockManager internal data
2524
# structure
@@ -352,7 +351,7 @@ def test_constructor_no_pandas_array(self):
352351
result = DataFrame({"A": arr})
353352
expected = DataFrame({"A": [1, 2, 3]})
354353
tm.assert_frame_equal(result, expected)
355-
assert isinstance(result._mgr.blocks[0], IntBlock)
354+
assert isinstance(result._mgr.blocks[0], NumericBlock)
356355

357356
def test_add_column_with_pandas_array(self):
358357
# GH 26390

pandas/tests/internals/test_internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1295,17 +1295,17 @@ def test_make_block_no_pandas_array():
12951295

12961296
# PandasArray, no dtype
12971297
result = make_block(arr, slice(len(arr)), ndim=arr.ndim)
1298-
assert result.is_integer is True
1298+
assert result.dtype.kind in ["i", "u"]
12991299
assert result.is_extension is False
13001300

13011301
# PandasArray, PandasDtype
13021302
result = make_block(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
1303-
assert result.is_integer is True
1303+
assert result.dtype.kind in ["i", "u"]
13041304
assert result.is_extension is False
13051305

13061306
# ndarray, PandasDtype
13071307
result = make_block(arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
1308-
assert result.is_integer is True
1308+
assert result.dtype.kind in ["i", "u"]
13091309
assert result.is_extension is False
13101310

13111311

pandas/tests/reshape/concat/test_concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_concat_copy(self):
3131
for b in result._mgr.blocks:
3232
if b.is_float:
3333
assert b.values.base is df._mgr.blocks[0].values.base
34-
elif b.is_integer:
34+
elif b.dtype.kind in ["i", "u"]:
3535
assert b.values.base is df2._mgr.blocks[0].values.base
3636
elif b.is_object:
3737
assert b.values.base is not None
@@ -42,7 +42,7 @@ def test_concat_copy(self):
4242
for b in result._mgr.blocks:
4343
if b.is_float:
4444
assert b.values.base is None
45-
elif b.is_integer:
45+
elif b.dtype.kind in ["i", "u"]:
4646
assert b.values.base is df2._mgr.blocks[0].values.base
4747
elif b.is_object:
4848
assert b.values.base is not None

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
)
3333
import pandas._testing as tm
3434
from pandas.core.arrays import IntervalArray, period_array
35-
from pandas.core.internals.blocks import IntBlock
35+
from pandas.core.internals.blocks import NumericBlock
3636

3737

3838
class TestSeriesConstructors:
@@ -1649,7 +1649,7 @@ def test_constructor_no_pandas_array(self):
16491649
ser = Series([1, 2, 3])
16501650
result = Series(ser.array)
16511651
tm.assert_series_equal(ser, result)
1652-
assert isinstance(result._mgr.blocks[0], IntBlock)
1652+
assert isinstance(result._mgr.blocks[0], NumericBlock)
16531653

16541654
def test_from_array(self):
16551655
result = Series(pd.array(["1H", "2H"], dtype="timedelta64[ns]"))

0 commit comments

Comments
 (0)