-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data] Optimizing the multi column groupby
Changed from a custom class implementation to a purely numpy implementation Signed-off-by: Kit Lee <7000003+wingkitlee0@users.noreply.github.com>
- Loading branch information
1 parent
e07594e
commit b6076d6
Showing
5 changed files
with
77 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Dict, Union | ||
|
||
import numpy as np | ||
|
||
|
||
def get_key_boundaries( | ||
keys: Union[np.ndarray, Dict[str, np.ndarray]], include_first: bool = True | ||
) -> np.ndarray: | ||
"""Compute block boundaries based on the key(s), that is, a list of | ||
starting indices of each group and a end index of the last group. | ||
Args: | ||
keys: numpy arrays of the group key(s). | ||
include_first: Whether to include the first index (0). | ||
Returns: | ||
A list of starting indices of each group. The first entry is 0 and | ||
the last entry is ``len(array)``. | ||
""" | ||
|
||
if isinstance(keys, dict): | ||
# For multiple keys, we create a numpy record array | ||
dtype = [(k, v.dtype) for k, v in keys.items()] | ||
keys = np.array(list(zip(*keys.values())), dtype=dtype) | ||
|
||
if include_first: | ||
return np.hstack([[0], np.where(keys[1:] != keys[:-1])[0] + 1, [len(keys)]]) | ||
else: | ||
return np.hstack([np.where(keys[1:] != keys[:-1])[0] + 1, [len(keys)]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import numpy as np | ||
|
||
from ray.data._internal.boundaries import get_key_boundaries | ||
|
||
|
||
def test_groupby_map_groups_get_key_boundaries(): | ||
indices = get_key_boundaries( | ||
keys={ | ||
"x": np.array([1, 1, 2, 2, 3, 3]), | ||
"y": np.array([1, 1, 2, 2, 3, 4]), | ||
} | ||
) | ||
|
||
assert list(indices) == [0, 2, 4, 5, 6] | ||
|
||
indices = get_key_boundaries( | ||
keys={ | ||
"x": np.array([1, 1, 2, 2, 3, 3]), | ||
"y": np.array(["a", "b", "a", "a", "b", "b"]), | ||
} | ||
) | ||
|
||
assert list(indices) == [0, 1, 2, 4, 6] | ||
|
||
indices = get_key_boundaries(np.array([1, 1, 2, 2, 3, 3])) | ||
|
||
assert list(indices) == [0, 2, 4, 6] |