Skip to content

Commit f542723

Browse files
authored
TYP: enforce tighter inputs on SingleBlockManager (#33092)
1 parent d7a100c commit f542723

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ Deprecations
253253
- 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`)
254254
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
255255
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
256+
- The ``fastpath`` keyword in the ``SingleBlockManager`` constructor is deprecated and will be removed in a future version (:issue:`33092`)
256257

257258
.. ---------------------------------------------------------------------------
258259

pandas/core/internals/managers.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import operator
55
import re
66
from typing import Dict, List, Optional, Sequence, Tuple, TypeVar, Union
7+
import warnings
78

89
import numpy as np
910

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

542543
return SingleBlockManager(
543-
make_block(values, ndim=1, placement=np.arange(len(values))),
544-
axes[0],
545-
fastpath=True,
544+
make_block(values, ndim=1, placement=np.arange(len(values))), axes[0],
546545
)
547546

548547
def isna(self, func) -> "BlockManager":
@@ -1001,7 +1000,6 @@ def iget(self, i: int) -> "SingleBlockManager":
10011000
values, placement=slice(0, len(values)), ndim=1
10021001
),
10031002
self.axes[1],
1004-
fastpath=True,
10051003
)
10061004

10071005
def delete(self, item):
@@ -1509,25 +1507,22 @@ class SingleBlockManager(BlockManager):
15091507
def __init__(
15101508
self,
15111509
block: Block,
1512-
axis: Union[Index, List[Index]],
1510+
axis: Index,
15131511
do_integrity_check: bool = False,
1514-
fastpath: bool = False,
1512+
fastpath=lib.no_default,
15151513
):
15161514
assert isinstance(block, Block), type(block)
1515+
assert isinstance(axis, Index), type(axis)
1516+
1517+
if fastpath is not lib.no_default:
1518+
warnings.warn(
1519+
"The `fastpath` keyword is deprecated and will be removed "
1520+
"in a future version.",
1521+
FutureWarning,
1522+
stacklevel=2,
1523+
)
15171524

1518-
if isinstance(axis, list):
1519-
if len(axis) != 1:
1520-
raise ValueError(
1521-
"cannot create SingleBlockManager with more than 1 axis"
1522-
)
1523-
axis = axis[0]
1524-
1525-
# passed from constructor, single block, single axis
1526-
if fastpath:
1527-
self.axes = [axis]
1528-
else:
1529-
self.axes = [ensure_index(axis)]
1530-
1525+
self.axes = [axis]
15311526
self.blocks = tuple([block])
15321527

15331528
@classmethod
@@ -1539,15 +1534,15 @@ def from_blocks(
15391534
"""
15401535
assert len(blocks) == 1
15411536
assert len(axes) == 1
1542-
return cls(blocks[0], axes[0], do_integrity_check=False, fastpath=True)
1537+
return cls(blocks[0], axes[0], do_integrity_check=False)
15431538

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

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

15781573
@property
15791574
def index(self) -> Index:
@@ -1627,7 +1622,9 @@ def fast_xs(self, loc):
16271622
"""
16281623
raise NotImplementedError("Use series._values[loc] instead")
16291624

1630-
def concat(self, to_concat, new_axis: Index) -> "SingleBlockManager":
1625+
def concat(
1626+
self, to_concat: List["SingleBlockManager"], new_axis: Index
1627+
) -> "SingleBlockManager":
16311628
"""
16321629
Concatenate a list of SingleBlockManagers into a single
16331630
SingleBlockManager.

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def get_result(self):
458458
name = com.consensus_name_attr(self.objs)
459459

460460
mgr = self.objs[0]._data.concat(
461-
[x._data for x in self.objs], self.new_axes
461+
[x._data for x in self.objs], self.new_axes[0]
462462
)
463463
cons = self.objs[0]._constructor
464464
return cons(mgr, name=name).__finalize__(self, method="concat")

pandas/core/series.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -971,9 +971,7 @@ def _get_values_tuple(self, key):
971971

972972
def _get_values(self, indexer):
973973
try:
974-
return self._constructor(
975-
self._data.get_slice(indexer), fastpath=True
976-
).__finalize__(self)
974+
return self._constructor(self._data.get_slice(indexer)).__finalize__(self)
977975
except ValueError:
978976
# mpl compat if we look up e.g. ser[:, np.newaxis];
979977
# see tests.series.timeseries.test_mpl_compat_hack

pandas/tests/internals/test_internals.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def create_single_mgr(typestr, num_rows=None):
135135

136136
return SingleBlockManager(
137137
create_block(typestr, placement=slice(0, num_rows), item_shape=()),
138-
np.arange(num_rows),
138+
Index(np.arange(num_rows)),
139139
)
140140

141141

@@ -1297,3 +1297,11 @@ def test_interleave_non_unique_cols():
12971297
assert df_unique.values.shape == df.values.shape
12981298
tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
12991299
tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])
1300+
1301+
1302+
def test_single_block_manager_fastpath_deprecated():
1303+
# GH#33092
1304+
ser = pd.Series(range(3))
1305+
blk = ser._data.blocks[0]
1306+
with tm.assert_produces_warning(FutureWarning):
1307+
SingleBlockManager(blk, ser.index, fastpath=True)

0 commit comments

Comments
 (0)