Skip to content

Commit 6e29dbf

Browse files
authored
REF: Refactor reference handling to the BlockManager level for iterrows (#51431)
1 parent 56dc877 commit 6e29dbf

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

pandas/core/frame.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1396,10 +1396,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
13961396
for k, v in zip(self.index, self.values):
13971397
s = klass(v, index=columns, name=k).__finalize__(self)
13981398
if using_cow and self._mgr.is_single_block:
1399-
s._mgr.blocks[0].refs = self._mgr.blocks[0].refs # type: ignore[union-attr] # noqa
1400-
s._mgr.blocks[0].refs.add_reference( # type: ignore[union-attr]
1401-
s._mgr.blocks[0] # type: ignore[arg-type, union-attr]
1402-
)
1399+
s._mgr.add_references(self._mgr) # type: ignore[arg-type]
14031400
yield k, s
14041401

14051402
def itertuples(

pandas/core/internals/array_manager.py

+6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
169169
def get_dtypes(self) -> np.ndarray:
170170
return np.array([arr.dtype for arr in self.arrays], dtype="object")
171171

172+
def add_references(self, mgr: BaseArrayManager) -> None:
173+
"""
174+
Only implemented on the BlockManager level
175+
"""
176+
return
177+
172178
def __getstate__(self):
173179
return self.arrays, self._axes
174180

pandas/core/internals/managers.py

+11
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ def _has_no_reference_block(self, blkno: int) -> bool:
247247
"""
248248
return not self.blocks[blkno].refs.has_reference()
249249

250+
def add_references(self, mgr: BaseBlockManager) -> None:
251+
"""
252+
Adds the references from one manager to another. We assume that both
253+
managers have the same block structure.
254+
"""
255+
for i, blk in enumerate(self.blocks):
256+
blk.refs = mgr.blocks[i].refs
257+
# Argument 1 to "add_reference" of "BlockValuesRefs" has incompatible type
258+
# "Block"; expected "SharedBlock"
259+
blk.refs.add_reference(blk) # type: ignore[arg-type]
260+
250261
def get_dtypes(self):
251262
dtypes = np.array([blk.dtype for blk in self.blocks])
252263
return dtypes.take(self.blknos)

0 commit comments

Comments
 (0)