Skip to content

PERF: column_arrays #42824

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

Closed
Closed
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
41 changes: 24 additions & 17 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,28 +981,35 @@ def iget_values(self, i: int) -> ArrayLike:
return values

@property
def column_arrays(self) -> list[np.ndarray]:
def column_arrays(self) -> list[ArrayLike]:
"""
Used in the JSON C code to access column arrays.
This optimizes compared to using `iget_values` by converting each
block.values to a np.ndarray only once up front
"""
# special casing datetimetz to avoid conversion through object dtype
arrays = [
blk.values._ndarray
if isinstance(blk, DatetimeTZBlock)
else np.asarray(blk.values)
for blk in self.blocks
]
result = []
for i in range(len(self.items)):
arr = arrays[self.blknos[i]]
if arr.ndim == 2:
values = arr[self.blklocs[i]]
# This is an optimized equivalent to
# result = [self.iget_values(i) for i in range(len(self.items))]
result: list[ArrayLike | None] = [None] * len(self.items)
for blk in self.blocks:
is_dtz = isinstance(blk, DatetimeTZBlock)
mgr_locs = blk._mgr_locs
values = blk.values
if values.ndim == 1:
# TODO(EA2D): special casing not needed with 2D EAs
result[mgr_locs.indexer] = values

else:
values = arr
result.append(values)
return result
for i, loc in enumerate(mgr_locs):
result[loc] = values[i]
if is_dtz:
# special casing datetimetz to avoid conversion through
# object dtype
# error: Item "ndarray[Any, Any]" of
# "Optional[Union[ExtensionArray, ndarray[Any, Any]]]"
# has no attribute "_ndarray"
result[loc] = result[loc]._ndarray # type: ignore[union-attr]
# error: Incompatible return value type (got "List[None]",
# expected "List[ndarray[Any, Any]]")
return result # type: ignore[return-value]

def iset(self, loc: int | slice | np.ndarray, value: ArrayLike):
"""
Expand Down