Skip to content

REF: Refactor reference handling to the BlockManager level for iterrows #51431

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
Feb 17, 2023
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
5 changes: 1 addition & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,10 +1396,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
for k, v in zip(self.index, self.values):
s = klass(v, index=columns, name=k).__finalize__(self)
if using_cow and self._mgr.is_single_block:
s._mgr.blocks[0].refs = self._mgr.blocks[0].refs # type: ignore[union-attr] # noqa
s._mgr.blocks[0].refs.add_reference( # type: ignore[union-attr]
s._mgr.blocks[0] # type: ignore[arg-type, union-attr]
)
s._mgr.add_references(self._mgr) # type: ignore[arg-type]
yield k, s

def itertuples(
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
def get_dtypes(self) -> np.ndarray:
return np.array([arr.dtype for arr in self.arrays], dtype="object")

def add_references(self, mgr: BaseArrayManager) -> None:
"""
Only implemented on the BlockManager level
"""
return

def __getstate__(self):
return self.arrays, self._axes

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ def _has_no_reference_block(self, blkno: int) -> bool:
"""
return not self.blocks[blkno].refs.has_reference()

def add_references(self, mgr: BaseBlockManager) -> None:
"""
Adds the references from one manager to another. We assume that both
managers have the same block structure.
"""
for i, blk in enumerate(self.blocks):
blk.refs = mgr.blocks[i].refs
# Argument 1 to "add_reference" of "BlockValuesRefs" has incompatible type
# "Block"; expected "SharedBlock"
blk.refs.add_reference(blk) # type: ignore[arg-type]

def get_dtypes(self):
dtypes = np.array([blk.dtype for blk in self.blocks])
return dtypes.take(self.blknos)
Expand Down