Skip to content

TYP: internals #32403

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
Mar 3, 2020
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
84 changes: 54 additions & 30 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas._libs.internals as libinternals
from pandas._libs.tslibs import Timedelta, conversion
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import DtypeObj
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -170,20 +171,20 @@ def _consolidate_key(self):
return (self._can_consolidate, self.dtype.name)

@property
def _is_single_block(self):
def _is_single_block(self) -> bool:
return self.ndim == 1

@property
def is_view(self):
def is_view(self) -> bool:
""" return a boolean if I am possibly a view """
return self.values.base is not None

@property
def is_datelike(self):
def is_datelike(self) -> bool:
""" return True if I am a non-datelike """
return self.is_datetime or self.is_timedelta

def is_categorical_astype(self, dtype):
def is_categorical_astype(self, dtype) -> bool:
"""
validate that we have a astypeable to categorical,
returns a boolean if we are a categorical
Expand Down Expand Up @@ -255,7 +256,7 @@ def mgr_locs(self, new_mgr_locs):
self._mgr_locs = new_mgr_locs

@property
def array_dtype(self):
def array_dtype(self) -> DtypeObj:
"""
the dtype to return if I want to construct this block as an
array
Expand Down Expand Up @@ -333,7 +334,7 @@ def dtype(self):
return self.values.dtype

@property
def ftype(self):
def ftype(self) -> str:
if getattr(self.values, "_pandas_ftype", False):
dtype = self.dtype.subtype
else:
Expand Down Expand Up @@ -367,7 +368,7 @@ def set(self, locs, values):
"""
self.values[locs] = values

def delete(self, loc):
def delete(self, loc) -> None:
"""
Delete given loc(-s) from block in-place.
"""
Expand Down Expand Up @@ -401,7 +402,7 @@ def _split_op_result(self, result) -> List["Block"]:

return [result]

def fillna(self, value, limit=None, inplace=False, downcast=None):
def fillna(self, value, limit=None, inplace: bool = False, downcast=None):
"""
fillna on the block with the value. If we fail, then convert to
ObjectBlock and try again
Expand Down Expand Up @@ -687,15 +688,21 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
return values

# block actions #
def copy(self, deep=True):
def copy(self, deep: bool = True):
""" copy constructor """
values = self.values
if deep:
values = values.copy()
return self.make_block_same_class(values, ndim=self.ndim)

def replace(
self, to_replace, value, inplace=False, filter=None, regex=False, convert=True
self,
to_replace,
value,
inplace: bool = False,
filter=None,
regex: bool = False,
convert: bool = True,
):
"""
replace the to_replace value with value, possible to create new
Expand Down Expand Up @@ -917,7 +924,15 @@ def setitem(self, indexer, value):
block = self.make_block(values)
return block

def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False):
def putmask(
self,
mask,
new,
align: bool = True,
inplace: bool = False,
axis: int = 0,
transpose: bool = False,
):
"""
putmask the data to the block; it is possible that we may create a
new dtype of block
Expand Down Expand Up @@ -1264,7 +1279,7 @@ def func(x):
blocks = [self.make_block_same_class(interp_values)]
return self._maybe_downcast(blocks, downcast)

def take_nd(self, indexer, axis, new_mgr_locs=None, fill_tuple=None):
def take_nd(self, indexer, axis: int, new_mgr_locs=None, fill_tuple=None):
"""
Take values according to indexer and return them as a block.bb

Expand Down Expand Up @@ -1305,7 +1320,7 @@ def diff(self, n: int, axis: int = 1) -> List["Block"]:
new_values = _block_shape(new_values, ndim=self.ndim)
return [self.make_block(values=new_values)]

def shift(self, periods, axis=0, fill_value=None):
def shift(self, periods, axis: int = 0, fill_value=None):
""" shift the block by periods, possibly upcast """
# convert integer to float if necessary. need to do a lot more than
# that, handle boolean etc also
Expand Down Expand Up @@ -1337,7 +1352,7 @@ def where(
self,
other,
cond,
align=True,
align: bool = True,
errors="raise",
try_cast: bool = False,
axis: int = 0,
Expand All @@ -1349,11 +1364,12 @@ def where(
----------
other : a ndarray/object
cond : the condition to respect
align : boolean, perform alignment on other/cond
align : bool, default True
Perform alignment on other/cond.
errors : str, {'raise', 'ignore'}, default 'raise'
- ``raise`` : allow exceptions to be raised
- ``ignore`` : suppress exceptions. On error return original object
axis : int
axis : int, default 0

Returns
-------
Expand Down Expand Up @@ -1485,7 +1501,7 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
blocks = [make_block(new_values, placement=new_placement)]
return blocks, mask

def quantile(self, qs, interpolation="linear", axis=0):
def quantile(self, qs, interpolation="linear", axis: int = 0):
"""
compute the quantiles of the

Expand Down Expand Up @@ -1542,7 +1558,13 @@ def quantile(self, qs, interpolation="linear", axis=0):
return make_block(result, placement=np.arange(len(result)), ndim=ndim)

def _replace_coerce(
self, to_replace, value, inplace=True, regex=False, convert=False, mask=None
self,
to_replace,
value,
inplace: bool = True,
regex: bool = False,
convert: bool = False,
mask=None,
):
"""
Replace value corresponding to the given boolean array with another
Expand All @@ -1554,7 +1576,7 @@ def _replace_coerce(
Scalar to replace or regular expression to match.
value : object
Replacement object.
inplace : bool, default False
inplace : bool, default True
Perform inplace modification.
regex : bool, default False
If true, perform regular expression substitution.
Expand Down Expand Up @@ -1641,7 +1663,9 @@ def set(self, locs, values, check=False):
assert locs.tolist() == [0]
self.values = values

def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False):
def putmask(
self, mask, new, align=True, inplace=False, axis=0, transpose=False,
):
"""
putmask the data to the block; we must be a single block and not
generate other blocks
Expand Down Expand Up @@ -1757,7 +1781,7 @@ def _can_hold_na(self):
return self._holder._can_hold_na

@property
def is_view(self):
def is_view(self) -> bool:
"""Extension arrays are never treated as views."""
return False

Expand Down Expand Up @@ -1822,7 +1846,7 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
# we are expected to return a 2-d ndarray
return values.reshape(1, len(values))

def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
def take_nd(self, indexer, axis: int = 0, new_mgr_locs=None, fill_tuple=None):
"""
Take values according to indexer and return them as a block.
"""
Expand Down Expand Up @@ -2083,7 +2107,7 @@ def to_native_types(
)
return formatter.get_result_as_array()

def should_store(self, value):
def should_store(self, value) -> bool:
# when inserting a column should not coerce integers to floats
# unnecessarily
return issubclass(value.dtype.type, np.floating) and value.dtype == self.dtype
Expand All @@ -2101,7 +2125,7 @@ def _can_hold_element(self, element: Any) -> bool:
element, (float, int, complex, np.float_, np.int_)
) and not isinstance(element, (bool, np.bool_))

def should_store(self, value):
def should_store(self, value) -> bool:
return issubclass(value.dtype.type, np.complexfloating)


Expand All @@ -2120,7 +2144,7 @@ def _can_hold_element(self, element: Any) -> bool:
)
return is_integer(element)

def should_store(self, value):
def should_store(self, value) -> bool:
return is_integer_dtype(value) and value.dtype == self.dtype


Expand Down Expand Up @@ -2258,7 +2282,7 @@ def to_native_types(
).reshape(i8values.shape)
return np.atleast_2d(result)

def should_store(self, value):
def should_store(self, value) -> bool:
return (
issubclass(value.dtype.type, np.datetime64)
and not is_datetime64tz_dtype(value)
Expand Down Expand Up @@ -2323,7 +2347,7 @@ def _maybe_coerce_values(self, values):
return values

@property
def is_view(self):
def is_view(self) -> bool:
""" return a boolean if I am possibly a view """
# check the ndarray values of the DatetimeIndex values
return self.values._data.base is not None
Expand Down Expand Up @@ -2510,7 +2534,7 @@ def fillna(self, value, **kwargs):
)
return super().fillna(value, **kwargs)

def should_store(self, value):
def should_store(self, value) -> bool:
return issubclass(
value.dtype.type, np.timedelta64
) and not is_extension_array_dtype(value)
Expand Down Expand Up @@ -2556,7 +2580,7 @@ def _can_hold_element(self, element: Any) -> bool:
return issubclass(tipo.type, np.bool_)
return isinstance(element, (bool, np.bool_))

def should_store(self, value):
def should_store(self, value) -> bool:
return issubclass(value.dtype.type, np.bool_) and not is_extension_array_dtype(
value
)
Expand Down Expand Up @@ -2648,7 +2672,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
def _can_hold_element(self, element: Any) -> bool:
return True

def should_store(self, value):
def should_store(self, value) -> bool:
return not (
issubclass(
value.dtype.type,
Expand Down
Loading