Skip to content

Commit f61a840

Browse files
committed
Refactor out _prepare_for_flox
1 parent 44f3851 commit f61a840

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

flox/aggregate_flox.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
from .xrutils import isnull
66

7+
def _prepare_for_flox(group_idx, array):
8+
"""
9+
Sort the input array once to save time.
10+
"""
11+
assert array.shape[-1] == group_idx.shape[0]
12+
issorted = (group_idx[:-1] <= group_idx[1:]).all()
13+
if issorted:
14+
ordered_array = array
15+
else:
16+
perm = group_idx.argsort(kind="stable")
17+
group_idx = group_idx[..., perm]
18+
ordered_array = array[..., perm]
19+
return group_idx, ordered_array
20+
721

822
def _np_grouped_op(group_idx, array, op, axis=-1, size=None, fill_value=None, dtype=None, out=None):
923
"""

flox/core.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_initialize_aggregation,
2020
generic_aggregate,
2121
)
22+
from .aggregate_flox import _prepare_for_flox
2223
from .cache import memoize
2324
from .xrutils import is_duck_array, is_duck_dask_array, isnull
2425

@@ -44,21 +45,6 @@ def _is_arg_reduction(func: str | Aggregation) -> bool:
4445
return False
4546

4647

47-
def _prepare_for_flox(group_idx, array):
48-
"""
49-
Sort the input array once to save time.
50-
"""
51-
assert array.shape[-1] == group_idx.shape[0]
52-
issorted = (group_idx[:-1] <= group_idx[1:]).all()
53-
if issorted:
54-
ordered_array = array
55-
else:
56-
perm = group_idx.argsort(kind="stable")
57-
group_idx = group_idx[..., perm]
58-
ordered_array = array[..., perm]
59-
return group_idx, ordered_array
60-
61-
6248
def _get_expected_groups(by, sort, *, raise_if_dask=True) -> pd.Index | None:
6349
if is_duck_dask_array(by):
6450
if raise_if_dask:

0 commit comments

Comments
 (0)