Skip to content

Commit e36b92c

Browse files
authored
CLN: follow-ups to #33340 (#33405)
1 parent d537d77 commit e36b92c

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

pandas/_libs/internals.pyx

+5-12
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,8 @@ cdef class BlockPlacement:
106106
else:
107107
return self._as_array
108108

109-
def isin(self, arr):
110-
from pandas.core.indexes.api import Int64Index
111-
112-
return Int64Index(self.as_array, copy=False).isin(arr)
113-
114109
@property
115-
def as_array(self):
110+
def as_array(self) -> np.ndarray:
116111
cdef:
117112
Py_ssize_t start, stop, end, _
118113

@@ -146,10 +141,10 @@ cdef class BlockPlacement:
146141

147142
return BlockPlacement(val)
148143

149-
def delete(self, loc):
144+
def delete(self, loc) -> "BlockPlacement":
150145
return BlockPlacement(np.delete(self.as_array, loc, axis=0))
151146

152-
def append(self, others):
147+
def append(self, others) -> "BlockPlacement":
153148
if not len(others):
154149
return self
155150

@@ -190,12 +185,10 @@ cdef class BlockPlacement:
190185
val = newarr
191186
return BlockPlacement(val)
192187

193-
def add(self, other):
188+
def add(self, other) -> "BlockPlacement":
189+
# We can get here with int or ndarray
194190
return self.iadd(other)
195191

196-
def sub(self, other):
197-
return self.add(-other)
198-
199192
cdef slice _ensure_has_slice(self):
200193
if not self._has_slice:
201194
self._as_slice = indexer_as_slice(self._as_array)

pandas/core/generic.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -5305,10 +5305,10 @@ def _check_inplace_setting(self, value) -> bool_t:
53055305
return True
53065306

53075307
def _get_numeric_data(self):
5308-
return self._constructor(self._mgr.get_numeric_data()).__finalize__(self,)
5308+
return self._constructor(self._mgr.get_numeric_data()).__finalize__(self)
53095309

53105310
def _get_bool_data(self):
5311-
return self._constructor(self._mgr.get_bool_data()).__finalize__(self,)
5311+
return self._constructor(self._mgr.get_bool_data()).__finalize__(self)
53125312

53135313
# ----------------------------------------------------------------------
53145314
# Internal Interface Methods
@@ -6542,21 +6542,16 @@ def replace(
65426542
if not self.size:
65436543
return self
65446544

6545-
new_data = self._mgr
65466545
if is_dict_like(to_replace):
65476546
if is_dict_like(value): # {'A' : NA} -> {'A' : 0}
6548-
res = self if inplace else self.copy()
6549-
for c, src in to_replace.items():
6550-
if c in value and c in self:
6551-
# object conversion is handled in
6552-
# series.replace which is called recursively
6553-
res[c] = res[c].replace(
6554-
to_replace=src,
6555-
value=value[c],
6556-
inplace=False,
6557-
regex=regex,
6558-
)
6559-
return None if inplace else res
6547+
# Note: Checking below for `in foo.keys()` instead of
6548+
# `in foo`is needed for when we have a Series and not dict
6549+
mapping = {
6550+
col: (to_replace[col], value[col])
6551+
for col in to_replace.keys()
6552+
if col in value.keys() and col in self
6553+
}
6554+
return self._replace_columnwise(mapping, inplace, regex)
65606555

65616556
# {'A': NA} -> 0
65626557
elif not is_list_like(value):

pandas/core/internals/managers.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ class BlockManager(PandasObject):
110110
__slots__ = [
111111
"axes",
112112
"blocks",
113-
"_ndim",
114-
"_shape",
115113
"_known_consolidated",
116114
"_is_consolidated",
117115
"_blknos",
@@ -759,9 +757,7 @@ def get_slice(self, slobj: slice, axis: int = 0) -> "BlockManager":
759757
if axis == 0:
760758
new_blocks = self._slice_take_blocks_ax0(slobj)
761759
elif axis == 1:
762-
_slicer = [slice(None)] * (axis + 1)
763-
_slicer[axis] = slobj
764-
slicer = tuple(_slicer)
760+
slicer = (slice(None), slobj)
765761
new_blocks = [blk.getitem_block(slicer) for blk in self.blocks]
766762
else:
767763
raise IndexError("Requested axis not found in manager")
@@ -1103,7 +1099,6 @@ def value_getitem(placement):
11031099
if len(val_locs) == len(blk.mgr_locs):
11041100
removed_blknos.append(blkno)
11051101
else:
1106-
self._blklocs[blk.mgr_locs.indexer] = -1
11071102
blk.delete(blk_locs)
11081103
self._blklocs[blk.mgr_locs.indexer] = np.arange(len(blk))
11091104

@@ -1115,9 +1110,7 @@ def value_getitem(placement):
11151110
new_blknos = np.empty(self.nblocks, dtype=np.int64)
11161111
new_blknos.fill(-1)
11171112
new_blknos[~is_deleted] = np.arange(self.nblocks - len(removed_blknos))
1118-
self._blknos = algos.take_1d(
1119-
new_blknos, self._blknos, axis=0, allow_fill=False
1120-
)
1113+
self._blknos = new_blknos[self._blknos]
11211114
self.blocks = tuple(
11221115
blk for i, blk in enumerate(self.blocks) if i not in set(removed_blknos)
11231116
)
@@ -1128,8 +1121,9 @@ def value_getitem(placement):
11281121

11291122
new_blocks: List[Block] = []
11301123
if value_is_extension_type:
1131-
# This code (ab-)uses the fact that sparse blocks contain only
1124+
# This code (ab-)uses the fact that EA blocks contain only
11321125
# one item.
1126+
# TODO(EA2D): special casing unnecessary with 2D EAs
11331127
new_blocks.extend(
11341128
make_block(
11351129
values=value,
@@ -1162,7 +1156,7 @@ def value_getitem(placement):
11621156
# Newly created block's dtype may already be present.
11631157
self._known_consolidated = False
11641158

1165-
def insert(self, loc: int, item, value, allow_duplicates: bool = False):
1159+
def insert(self, loc: int, item: Label, value, allow_duplicates: bool = False):
11661160
"""
11671161
Insert item at selected position.
11681162
@@ -1185,7 +1179,7 @@ def insert(self, loc: int, item, value, allow_duplicates: bool = False):
11851179
# insert to the axis; this could possibly raise a TypeError
11861180
new_axis = self.items.insert(loc, item)
11871181

1188-
if value.ndim == self.ndim - 1 and not is_extension_array_dtype(value):
1182+
if value.ndim == self.ndim - 1 and not is_extension_array_dtype(value.dtype):
11891183
value = _safe_reshape(value, (1,) + value.shape)
11901184

11911185
block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
@@ -1959,14 +1953,14 @@ def _compare_or_regex_search(a, b, regex=False):
19591953
return result
19601954

19611955

1962-
def _fast_count_smallints(arr):
1956+
def _fast_count_smallints(arr: np.ndarray) -> np.ndarray:
19631957
"""Faster version of set(arr) for sequences of small numbers."""
19641958
counts = np.bincount(arr.astype(np.int_))
19651959
nz = counts.nonzero()[0]
19661960
return np.c_[nz, counts[nz]]
19671961

19681962

1969-
def _preprocess_slice_or_indexer(slice_or_indexer, length, allow_fill):
1963+
def _preprocess_slice_or_indexer(slice_or_indexer, length: int, allow_fill: bool):
19701964
if isinstance(slice_or_indexer, slice):
19711965
return (
19721966
"slice",

0 commit comments

Comments
 (0)