Skip to content

Add type hints for (BlockManager|SingleBlockManager).blocks #26888

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
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
18 changes: 12 additions & 6 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, Union
from typing import List, Optional, Sequence, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -95,9 +95,12 @@ class BlockManager(PandasObject):
__slots__ = ['axes', 'blocks', '_ndim', '_shape', '_known_consolidated',
'_is_consolidated', '_blknos', '_blklocs']

def __init__(self, blocks, axes, do_integrity_check=True):
def __init__(self,
blocks: Sequence[Block],
axes: Sequence[Index],
do_integrity_check: bool = True):
self.axes = [ensure_index(ax) for ax in axes]
self.blocks = tuple(blocks)
self.blocks = tuple(blocks) # type: Tuple[Block, ...]

for block in blocks:
if block.is_sparse:
Expand Down Expand Up @@ -1415,8 +1418,11 @@ class SingleBlockManager(BlockManager):
_known_consolidated = True
__slots__ = ()

def __init__(self, block, axis, do_integrity_check=False, fastpath=False):

def __init__(self,
block: Block,
axis: Union[Index, List[Index]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really? when is this an actual Index?

Copy link
Contributor Author

@topper-123 topper-123 Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be either. For example, when printing a DataFrame, it is supplied either a index or a list of a single index in various stages of the printing operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be more consistent that it'd pick one of the two options. I'd prefer axis: Index, given that this is named axis (vs. axes for BlockManager.__init__).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I think this was for consistency with a BlockManger where you have 2 axes; ok with changing this (followup PR); not sure how much this would take to make consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was for consistency with Panel and 4dPandel etc, which isnt relevant anymore. Ill look into changing it.

do_integrity_check: bool = False,
fastpath: bool = False):
if isinstance(axis, list):
if len(axis) != 1:
raise ValueError("cannot create SingleBlockManager with more "
Expand Down Expand Up @@ -1455,7 +1461,7 @@ def __init__(self, block, axis, do_integrity_check=False, fastpath=False):
if not isinstance(block, Block):
block = make_block(block, placement=slice(0, len(axis)), ndim=1)

self.blocks = [block]
self.blocks = tuple([block])

def _post_setstate(self):
pass
Expand Down