Skip to content

Commit f703cab

Browse files
authored
PERF: Use reshape instead of ravel/flatten (#58972)
* Use reshape instead of flatten * Use reshape instead of ravel' * add back tuple
1 parent c1dcd54 commit f703cab

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2970,7 +2970,7 @@ def transpose_homogeneous_pyarrow(
29702970
"""
29712971
arrays = list(arrays)
29722972
nrows, ncols = len(arrays[0]), len(arrays)
2973-
indices = np.arange(nrows * ncols).reshape(ncols, nrows).T.flatten()
2973+
indices = np.arange(nrows * ncols).reshape(ncols, nrows).T.reshape(-1)
29742974
arr = pa.chunked_array([chunk for arr in arrays for chunk in arr._pa_array.chunks])
29752975
arr = arr.take(indices)
29762976
return [ArrowExtensionArray(arr.slice(i * ncols, ncols)) for i in range(nrows)]

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9271,7 +9271,9 @@ def compare(
92719271

92729272
# reorder axis to keep things organized
92739273
indices = (
9274-
np.arange(diff.shape[axis]).reshape([2, diff.shape[axis] // 2]).T.flatten()
9274+
np.arange(diff.shape[axis])
9275+
.reshape([2, diff.shape[axis] // 2])
9276+
.T.reshape(-1)
92759277
)
92769278
diff = diff.take(indices, axis=axis)
92779279

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ def _align_frame(self, indexer, df: DataFrame) -> DataFrame:
24402440
ax = self.obj.axes[i]
24412441
if is_sequence(ix) or isinstance(ix, slice):
24422442
if isinstance(ix, np.ndarray):
2443-
ix = ix.ravel()
2443+
ix = ix.reshape(-1)
24442444
if idx is None:
24452445
idx = ax[ix]
24462446
elif cols is None:

pandas/core/reshape/reshape.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ def _convert_level_number(level_num: int, columns: Index):
842842
[x._values.astype(dtype, copy=False) for _, x in subset.items()]
843843
)
844844
N, K = subset.shape
845-
idx = np.arange(N * K).reshape(K, N).T.ravel()
845+
idx = np.arange(N * K).reshape(K, N).T.reshape(-1)
846846
value_slice = value_slice.take(idx)
847847
else:
848848
value_slice = subset.values
@@ -924,7 +924,7 @@ def _reorder_for_extension_array_stack(
924924
# idx is an indexer like
925925
# [c0r0, c1r0, c2r0, ...,
926926
# c0r1, c1r1, c2r1, ...]
927-
idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.ravel()
927+
idx = np.arange(n_rows * n_columns).reshape(n_columns, n_rows).T.reshape(-1)
928928
return arr.take(idx)
929929

930930

0 commit comments

Comments
 (0)