Skip to content

CLN: get_flattened_iterator #35515

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 17 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def _get_group_keys(self):
comp_ids, _, ngroups = self.group_info

# provide "flattened" iterator for multi-group setting
return get_flattened_iterator(comp_ids, ngroups, self.levels, self.codes)
return list(
get_flattened_iterator(comp_ids, ngroups, self.levels, self.codes)
)

def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
mutated = self.mutated
Expand Down
44 changes: 13 additions & 31 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" miscellaneous sorting / groupby utilities """
from typing import Callable, Optional
from typing import Callable, Generator, List, Optional, Tuple

import numpy as np

Expand Down Expand Up @@ -440,36 +440,18 @@ def ensure_key_mapped(values, key: Optional[Callable], levels=None):
return result


class _KeyMapper:
"""
Map compressed group id -> key tuple.
"""

def __init__(self, comp_ids, ngroups: int, levels, labels):
self.levels = levels
self.labels = labels
self.comp_ids = comp_ids.astype(np.int64)

self.k = len(labels)
self.tables = [hashtable.Int64HashTable(ngroups) for _ in range(self.k)]

self._populate_tables()

def _populate_tables(self):
for labs, table in zip(self.labels, self.tables):
table.map(self.comp_ids, labs.astype(np.int64))

def get_key(self, comp_id):
return tuple(
level[table.get_item(comp_id)]
for table, level in zip(self.tables, self.levels)
)


def get_flattened_iterator(comp_ids, ngroups, levels, labels):
# provide "flattened" iterator for multi-group setting
mapper = _KeyMapper(comp_ids, ngroups, levels, labels)
return [mapper.get_key(i) for i in range(ngroups)]
def get_flattened_iterator(
Copy link
Member

Choose a reason for hiding this comment

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

good cleanup here, but how about make this get_flattened_list and not need to cast in the calling function?

comp_ids: np.ndarray, ngroups: int, levels, labels: List[np.ndarray]
) -> Generator[Tuple, None, None]:
"""Map compressed group id -> key tuple."""
comp_ids = comp_ids.astype(np.int64, copy=False)
tables = []
for labs, level in zip(labels, levels):
table = hashtable.Int64HashTable(ngroups)
table.map(comp_ids, labs.astype(np.int64, copy=False))
Copy link
Contributor

Choose a reason for hiding this comment

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

you could make this a list-comprehension, maybe it would be slightly less readable though

tables.append(table)
for i in range(ngroups):
yield tuple(level[table.get_item(i)] for table, level in zip(tables, levels))


def get_indexer_dict(label_list, keys):
Expand Down