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 7 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: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from pandas.core.sorting import (
compress_group_index,
decons_obs_group_ids,
get_flattened_iterator,
get_flattened_list,
get_group_index,
get_group_index_sorter,
get_indexer_dict,
Expand Down Expand Up @@ -153,7 +153,7 @@ 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 get_flattened_list(comp_ids, ngroups, self.levels, self.codes)

def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
mutated = self.mutated
Expand Down
46 changes: 15 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, List, Optional, Tuple

import numpy as np

Expand Down Expand Up @@ -440,36 +440,20 @@ 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_list(
comp_ids: np.ndarray, ngroups: int, levels, labels: List[np.ndarray]
) -> List[Tuple]:
"""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)
return [
tuple(level[table.get_item(i)] for table, level in zip(tables, levels))
Copy link
Member

Choose a reason for hiding this comment

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

Is there any performance difference in creating an intermediary list to store the result of zip(tables, levels) rather than doing it only the fly in each iteration here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Eliminates a loop iteration and storing these table objects

for i in range(ngroups)
]


def get_indexer_dict(label_list, keys):
Expand Down