Skip to content

CLN: internals.managers #31316

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 1 commit into from
Jan 26, 2020
Merged
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
65 changes: 32 additions & 33 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import itertools
import operator
import re
from typing import List, Optional, Sequence, Tuple, Union
from typing import Dict, List, Optional, Sequence, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -181,23 +181,23 @@ def set_axis(self, axis, new_labels):

self.axes[axis] = new_labels

def rename_axis(self, mapper, axis, copy=True, level=None):
def rename_axis(self, mapper, axis, copy: bool = True, level=None):
"""
Rename one of axes.

Parameters
----------
mapper : unary callable
axis : int
copy : boolean, default True
copy : bool, default True
level : int, default None
"""
obj = self.copy(deep=copy)
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
return obj

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

Expand Down Expand Up @@ -441,9 +441,9 @@ def quantile(
Parameters
----------
axis: reduction axis, default 0
consolidate: boolean, default True. Join together blocks having same
consolidate: bool, default True. Join together blocks having same
dtype
transposed: boolean, default False
transposed: bool, default False
we are holding transposed data
interpolation : type of interpolation, default 'linear'
qs : a scalar or list of the quantiles to be computed
Expand Down Expand Up @@ -525,7 +525,9 @@ def get_axe(block, qs, axes):
values = values.take(indexer)

return SingleBlockManager(
[make_block(values, ndim=1, placement=np.arange(len(values)))], axes[0]
make_block(values, ndim=1, placement=np.arange(len(values))),
axes[0],
fastpath=True,
)

def isna(self, func):
Expand Down Expand Up @@ -635,24 +637,24 @@ def _consolidate_check(self):
self._known_consolidated = True

@property
def is_mixed_type(self):
def is_mixed_type(self) -> bool:
# Warning, consolidation needs to get checked upstairs
self._consolidate_inplace()
return len(self.blocks) > 1

@property
def is_numeric_mixed_type(self):
def is_numeric_mixed_type(self) -> bool:
# Warning, consolidation needs to get checked upstairs
self._consolidate_inplace()
return all(block.is_numeric for block in self.blocks)

@property
def any_extension_types(self):
def any_extension_types(self) -> bool:
"""Whether any of the blocks in this manager are extension blocks"""
return any(block.is_extension for block in self.blocks)

@property
def is_view(self):
def is_view(self) -> bool:
""" return a boolean if we are a single block and are a view """
if len(self.blocks) == 1:
return self.blocks[0].is_view
Expand All @@ -666,21 +668,21 @@ def is_view(self):

return False

def get_bool_data(self, copy=False):
def get_bool_data(self, copy: bool = False):
"""
Parameters
----------
copy : boolean, default False
copy : bool, default False
Whether to copy the blocks
"""
self._consolidate_inplace()
return self.combine([b for b in self.blocks if b.is_bool], copy)

def get_numeric_data(self, copy=False):
def get_numeric_data(self, copy: bool = False):
"""
Parameters
----------
copy : boolean, default False
copy : bool, default False
Whether to copy the blocks
"""
self._consolidate_inplace()
Expand Down Expand Up @@ -772,8 +774,8 @@ def as_array(self, transpose: bool = False) -> np.ndarray:

Parameters
----------
transpose : boolean, default False
If True, transpose the return array
transpose : bool, default False
If True, transpose the return array,

Returns
-------
Expand Down Expand Up @@ -825,13 +827,13 @@ def _interleave(self):

return result

def to_dict(self, copy=True):
def to_dict(self, copy: bool = True):
"""
Return a dict of str(dtype) -> BlockManager

Parameters
----------
copy : boolean, default True
copy : bool, default True

Returns
-------
Expand All @@ -843,7 +845,7 @@ def to_dict(self, copy=True):
"""
self._consolidate_inplace()

bd = {}
bd: Dict[str, List[Block]] = {}
for b in self.blocks:
bd.setdefault(str(b.dtype), []).append(b)

Expand Down Expand Up @@ -944,21 +946,18 @@ def get(self, item):

def iget(self, i):
"""
Return the data as a SingleBlockManager if possible

Otherwise return as a ndarray
Return the data as a SingleBlockManager.
"""
block = self.blocks[self._blknos[i]]
values = block.iget(self._blklocs[i])

# shortcut for select a single-dim from a 2-dim BM
return SingleBlockManager(
[
block.make_block_same_class(
values, placement=slice(0, len(values)), ndim=1
)
],
block.make_block_same_class(
values, placement=slice(0, len(values)), ndim=1
),
self.axes[1],
fastpath=True,
)

def delete(self, item):
Expand Down Expand Up @@ -1360,7 +1359,7 @@ def take(self, indexer, axis=1, verify=True, convert=True):
new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True
)

def equals(self, other):
def equals(self, other) -> bool:
self_axes, other_axes = self.axes, other.axes
if len(self_axes) != len(other_axes):
return False
Expand All @@ -1385,7 +1384,8 @@ def canonicalize(block):
)

def unstack(self, unstacker_func, fill_value):
"""Return a blockmanager with all blocks unstacked.
"""
Return a BlockManager with all blocks unstacked..

Parameters
----------
Expand Down Expand Up @@ -1538,7 +1538,7 @@ def get_values(self):
return np.array(self._block.to_dense(), copy=False)

@property
def _can_hold_na(self):
def _can_hold_na(self) -> bool:
return self._block._can_hold_na

def is_consolidated(self):
Expand Down Expand Up @@ -1567,7 +1567,7 @@ def fast_xs(self, loc):
"""
return self._block.values[loc]

def concat(self, to_concat, new_axis):
def concat(self, to_concat, new_axis) -> "SingleBlockManager":
"""
Concatenate a list of SingleBlockManagers into a single
SingleBlockManager.
Expand All @@ -1582,7 +1582,6 @@ def concat(self, to_concat, new_axis):
Returns
-------
SingleBlockManager

"""
non_empties = [x for x in to_concat if len(x) > 0]

Expand Down