Skip to content

TYP: enforce tighter inputs on SingleBlockManager #33092

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
Mar 30, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ Deprecations
- Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
- The ``fastpath`` keyword in the ``SingleBlockManager`` constructor is deprecated and will be removed in a future version (:issue:`33092`)

.. ---------------------------------------------------------------------------

Expand Down
43 changes: 20 additions & 23 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import operator
import re
from typing import Dict, List, Optional, Sequence, Tuple, TypeVar, Union
import warnings

import numpy as np

Expand Down Expand Up @@ -540,9 +541,7 @@ def get_axe(block, qs, axes):
values = values.take(indexer)

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

def isna(self, func) -> "BlockManager":
Expand Down Expand Up @@ -1001,7 +1000,6 @@ def iget(self, i: int) -> "SingleBlockManager":
values, placement=slice(0, len(values)), ndim=1
),
self.axes[1],
fastpath=True,
)

def delete(self, item):
Expand Down Expand Up @@ -1509,25 +1507,22 @@ class SingleBlockManager(BlockManager):
def __init__(
self,
block: Block,
axis: Union[Index, List[Index]],
axis: Index,
do_integrity_check: bool = False,
fastpath: bool = False,
fastpath=lib.no_default,
):
assert isinstance(block, Block), type(block)
assert isinstance(axis, Index), type(axis)

if fastpath is not lib.no_default:
warnings.warn(
"The `fastpath` keyword is deprecated and will be removed "
"in a future version.",
FutureWarning,
stacklevel=2,
)

if isinstance(axis, list):
if len(axis) != 1:
raise ValueError(
"cannot create SingleBlockManager with more than 1 axis"
)
axis = axis[0]

# passed from constructor, single block, single axis
if fastpath:
self.axes = [axis]
else:
self.axes = [ensure_index(axis)]

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

@classmethod
Expand All @@ -1539,15 +1534,15 @@ def from_blocks(
"""
assert len(blocks) == 1
assert len(axes) == 1
return cls(blocks[0], axes[0], do_integrity_check=False, fastpath=True)
return cls(blocks[0], axes[0], do_integrity_check=False)

@classmethod
def from_array(cls, array: ArrayLike, index: Index) -> "SingleBlockManager":
"""
Constructor for if we have an array that is not yet a Block.
"""
block = make_block(array, placement=slice(0, len(index)), ndim=1)
return cls(block, index, fastpath=True)
return cls(block, index)

def _post_setstate(self):
pass
Expand All @@ -1573,7 +1568,7 @@ def get_slice(self, slobj: slice, axis: int = 0) -> "SingleBlockManager":
blk = self._block
array = blk._slice(slobj)
block = blk.make_block_same_class(array, placement=range(len(array)))
return type(self)(block, self.index[slobj], fastpath=True)
return type(self)(block, self.index[slobj])

@property
def index(self) -> Index:
Expand Down Expand Up @@ -1627,7 +1622,9 @@ def fast_xs(self, loc):
"""
raise NotImplementedError("Use series._values[loc] instead")

def concat(self, to_concat, new_axis: Index) -> "SingleBlockManager":
def concat(
self, to_concat: List["SingleBlockManager"], new_axis: Index
) -> "SingleBlockManager":
"""
Concatenate a list of SingleBlockManagers into a single
SingleBlockManager.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def get_result(self):
name = com.consensus_name_attr(self.objs)

mgr = self.objs[0]._data.concat(
[x._data for x in self.objs], self.new_axes
[x._data for x in self.objs], self.new_axes[0]
)
cons = self.objs[0]._constructor
return cons(mgr, name=name).__finalize__(self, method="concat")
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,7 @@ def _get_values_tuple(self, key):

def _get_values(self, indexer):
try:
return self._constructor(
self._data.get_slice(indexer), fastpath=True
).__finalize__(self)
return self._constructor(self._data.get_slice(indexer)).__finalize__(self)
except ValueError:
# mpl compat if we look up e.g. ser[:, np.newaxis];
# see tests.series.timeseries.test_mpl_compat_hack
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def create_single_mgr(typestr, num_rows=None):

return SingleBlockManager(
create_block(typestr, placement=slice(0, num_rows), item_shape=()),
np.arange(num_rows),
Index(np.arange(num_rows)),
)


Expand Down Expand Up @@ -1297,3 +1297,11 @@ def test_interleave_non_unique_cols():
assert df_unique.values.shape == df.values.shape
tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])


def test_single_block_manager_fastpath_deprecated():
# GH#33092
ser = pd.Series(range(3))
blk = ser._data.blocks[0]
with tm.assert_produces_warning(FutureWarning):
SingleBlockManager(blk, ser.index, fastpath=True)