Skip to content

PERF: specialized 1D take version #40246

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
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions pandas/core/array_algos/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,43 @@ def _take_nd_ndarray(
return out


def take_1d(
arr: ArrayLike,
indexer: np.ndarray,
fill_value=None,
allow_fill: bool = True,
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> ArrayLike?

"""
Specialized version for 1D arrays. Differences compared to take_nd:

- Assumes input (arr, indexer) has already been converted to numpy array / EA
- Only works for 1D arrays

To ensure the lowest possible overhead.

TODO(ArrayManager): mainly useful for ArrayManager, otherwise can potentially
be removed again if we don't end up with ArrayManager.
"""
if not isinstance(arr, np.ndarray):
# ExtensionArray -> dispatch to their method
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)

indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
arr, indexer, 0, None, fill_value, allow_fill
)

# at this point, it's guaranteed that dtype can hold both the arr values
# and the fill_value
out = np.empty(indexer.shape, dtype=dtype)

func = _get_take_nd_function(
arr.ndim, arr.dtype, out.dtype, axis=0, mask_info=mask_info
)
func(arr, indexer, out, fill_value)

return out


def take_2d_multi(
arr: np.ndarray, indexer: np.ndarray, fill_value=np.nan
) -> np.ndarray:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

import pandas.core.algorithms as algos
from pandas.core.array_algos.quantile import quantile_compat
from pandas.core.array_algos.take import take_nd
from pandas.core.array_algos.take import take_1d
from pandas.core.arrays import (
DatetimeArray,
ExtensionArray,
Expand Down Expand Up @@ -1048,7 +1048,7 @@ def unstack(self, unstacker, fill_value) -> ArrayManager:
new_arrays = []
for arr in self.arrays:
for i in range(unstacker.full_shape[1]):
new_arr = take_nd(
new_arr = take_1d(
arr, new_indexer2D[:, i], allow_fill=True, fill_value=fill_value
)
new_arrays.append(new_arr)
Expand Down