Skip to content

REF: roll IntBlock, BoolBlock, ComplexBlock into NumericBlock #39236

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 5 commits into from
Jan 19, 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
8 changes: 2 additions & 6 deletions pandas/core/internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from pandas.core.internals.base import DataManager
from pandas.core.internals.blocks import ( # io.pytables, io.packers
Block,
BoolBlock,
CategoricalBlock,
ComplexBlock,
DatetimeBlock,
DatetimeTZBlock,
ExtensionBlock,
FloatBlock,
IntBlock,
NumericBlock,
ObjectBlock,
TimeDeltaBlock,
make_block,
Expand All @@ -25,14 +23,12 @@

__all__ = [
"Block",
"BoolBlock",
"CategoricalBlock",
"ComplexBlock",
"NumericBlock",
"DatetimeBlock",
"DatetimeTZBlock",
"ExtensionBlock",
"FloatBlock",
"IntBlock",
"ObjectBlock",
"TimeDeltaBlock",
"safe_reshape",
Expand Down
44 changes: 14 additions & 30 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ class Block(PandasObject):
__slots__ = ["_mgr_locs", "values", "ndim"]
is_numeric = False
is_float = False
is_integer = False
is_complex = False
is_datetime = False
is_datetimetz = False
is_timedelta = False
Expand Down Expand Up @@ -1195,7 +1193,7 @@ def _interpolate(

# only deal with floats
if not self.is_float:
if not self.is_integer:
if self.dtype.kind not in ["i", "u"]:
return [self]
data = data.astype(np.float64)

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

if not (
(self.is_integer or self.is_bool)
(self.dtype.kind in ["b", "i", "u"])
and lib.is_float(other)
and np.isnan(other)
):
Expand Down Expand Up @@ -1912,11 +1910,18 @@ def external_values(self):
class NumericBlock(Block):
__slots__ = ()
is_numeric = True
_can_hold_na = True

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

@property
def _can_hold_na(self):
return self.dtype.kind not in ["b", "i", "u"]

@property
def is_bool(self):
return self.dtype.kind == "b"


class FloatBlock(NumericBlock):
__slots__ = ()
Expand Down Expand Up @@ -1956,17 +1961,6 @@ def to_native_types(
return self.make_block(res)


class ComplexBlock(NumericBlock):
__slots__ = ()
is_complex = True


class IntBlock(NumericBlock):
__slots__ = ()
is_integer = True
_can_hold_na = False


class DatetimeLikeBlockMixin(HybridMixin, Block):
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""

Expand Down Expand Up @@ -2232,7 +2226,7 @@ def _check_ndim(self, values, ndim):
return ndim


class TimeDeltaBlock(DatetimeLikeBlockMixin, IntBlock):
class TimeDeltaBlock(DatetimeLikeBlockMixin):
__slots__ = ()
is_timedelta = True
_can_hold_na = True
Expand Down Expand Up @@ -2269,12 +2263,6 @@ def fillna(self, value, **kwargs):
return super().fillna(value, **kwargs)


class BoolBlock(NumericBlock):
__slots__ = ()
is_bool = True
_can_hold_na = False


class ObjectBlock(Block):
__slots__ = ()
is_object = True
Expand Down Expand Up @@ -2477,12 +2465,8 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
cls = TimeDeltaBlock
elif kind == "f":
cls = FloatBlock
elif kind == "c":
cls = ComplexBlock
elif kind == "i" or kind == "u":
cls = IntBlock
elif kind == "b":
cls = BoolBlock
elif kind in ["c", "i", "u", "b"]:
cls = NumericBlock
else:
cls = ObjectBlock
return cls
Expand Down
12 changes: 2 additions & 10 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,18 +1732,14 @@ def _form_blocks(arrays, names: Index, axes) -> List[Block]:
float_blocks = _multi_blockify(items_dict["FloatBlock"])
blocks.extend(float_blocks)

if len(items_dict["ComplexBlock"]):
complex_blocks = _multi_blockify(items_dict["ComplexBlock"])
if len(items_dict["NumericBlock"]):
complex_blocks = _multi_blockify(items_dict["NumericBlock"])
blocks.extend(complex_blocks)

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

if len(items_dict["IntBlock"]):
int_blocks = _multi_blockify(items_dict["IntBlock"])
blocks.extend(int_blocks)

if len(items_dict["DatetimeBlock"]):
datetime_blocks = _simple_blockify(items_dict["DatetimeBlock"], DT64NS_DTYPE)
blocks.extend(datetime_blocks)
Expand All @@ -1755,10 +1751,6 @@ def _form_blocks(arrays, names: Index, axes) -> List[Block]:
]
blocks.extend(dttz_blocks)

if len(items_dict["BoolBlock"]):
bool_blocks = _simple_blockify(items_dict["BoolBlock"], np.bool_)
blocks.extend(bool_blocks)

if len(items_dict["ObjectBlock"]) > 0:
object_blocks = _simple_blockify(items_dict["ObjectBlock"], np.object_)
blocks.extend(object_blocks)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
option_context,
)
import pandas._testing as tm
from pandas.core.internals import ObjectBlock
from pandas.core.internals.blocks import IntBlock
from pandas.core.internals import NumericBlock, ObjectBlock

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

def test_add_column_with_pandas_array(self):
# GH 26390
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,17 +1295,17 @@ def test_make_block_no_pandas_array():

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

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

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


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_concat_copy(self):
for b in result._mgr.blocks:
if b.is_float:
assert b.values.base is df._mgr.blocks[0].values.base
elif b.is_integer:
elif b.dtype.kind in ["i", "u"]:
assert b.values.base is df2._mgr.blocks[0].values.base
elif b.is_object:
assert b.values.base is not None
Expand All @@ -42,7 +42,7 @@ def test_concat_copy(self):
for b in result._mgr.blocks:
if b.is_float:
assert b.values.base is None
elif b.is_integer:
elif b.dtype.kind in ["i", "u"]:
assert b.values.base is df2._mgr.blocks[0].values.base
elif b.is_object:
assert b.values.base is not None
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)
import pandas._testing as tm
from pandas.core.arrays import IntervalArray, period_array
from pandas.core.internals.blocks import IntBlock
from pandas.core.internals.blocks import NumericBlock


class TestSeriesConstructors:
Expand Down Expand Up @@ -1649,7 +1649,7 @@ def test_constructor_no_pandas_array(self):
ser = Series([1, 2, 3])
result = Series(ser.array)
tm.assert_series_equal(ser, result)
assert isinstance(result._mgr.blocks[0], IntBlock)
assert isinstance(result._mgr.blocks[0], NumericBlock)

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