Skip to content

PERF: Use reshape instead of ravel/flatten #58972

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 4 commits into from
Jun 14, 2024
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2970,7 +2970,7 @@ def transpose_homogeneous_pyarrow(
"""
arrays = list(arrays)
nrows, ncols = len(arrays[0]), len(arrays)
indices = np.arange(nrows * ncols).reshape(ncols, nrows).T.flatten()
indices = np.arange(nrows * ncols).reshape(ncols, nrows).T.reshape(-1)
arr = pa.chunked_array([chunk for arr in arrays for chunk in arr._pa_array.chunks])
arr = arr.take(indices)
return [ArrowExtensionArray(arr.slice(i * ncols, ncols)) for i in range(nrows)]
4 changes: 3 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9271,7 +9271,9 @@ def compare(

# reorder axis to keep things organized
indices = (
np.arange(diff.shape[axis]).reshape([2, diff.shape[axis] // 2]).T.flatten()
np.arange(diff.shape[axis])
.reshape([2, diff.shape[axis] // 2])
.T.reshape(-1)
)
diff = diff.take(indices, axis=axis)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ def _align_frame(self, indexer, df: DataFrame) -> DataFrame:
ax = self.obj.axes[i]
if is_sequence(ix) or isinstance(ix, slice):
if isinstance(ix, np.ndarray):
ix = ix.ravel()
ix = ix.reshape(-1)
if idx is None:
idx = ax[ix]
elif cols is None:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ def _convert_level_number(level_num: int, columns: Index):
[x._values.astype(dtype, copy=False) for _, x in subset.items()]
)
N, K = subset.shape
idx = np.arange(N * K).reshape(K, N).T.ravel()
idx = np.arange(N * K).reshape(K, N).T.reshape(-1)
value_slice = value_slice.take(idx)
else:
value_slice = subset.values
Expand Down Expand Up @@ -924,7 +924,7 @@ def _reorder_for_extension_array_stack(
# idx is an indexer like
# [c0r0, c1r0, c2r0, ...,
# c0r1, c1r1, c2r1, ...]
idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.ravel()
idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.reshape(-1)
return arr.take(idx)


Expand Down