Skip to content

TYP/CLN: factorize_from_iterable(s) #40775

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 5 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 14 additions & 12 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
List,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -2642,13 +2643,11 @@ def recode_for_categories(
return new_codes


def factorize_from_iterable(values):
def factorize_from_iterable(values) -> Tuple[np.ndarray, Index]:
"""
Factorize an input `values` into `categories` and `codes`. Preserves
categorical dtype in `categories`.

*This is an internal function*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needless info: by not being exposed outside of pd.core makes this an internal function. No need to be explicit.

Parameters
----------
values : list-like
Expand All @@ -2660,6 +2659,8 @@ def factorize_from_iterable(values):
If `values` has a categorical dtype, then `categories` is
a CategoricalIndex keeping the categories and order of `values`.
"""
from pandas import CategoricalIndex

if not is_list_like(values):
raise TypeError("Input must be list-like")

Expand All @@ -2668,7 +2669,8 @@ def factorize_from_iterable(values):
# The Categorical we want to build has the same categories
# as values but its codes are by def [0, ..., len(n_categories) - 1]
cat_codes = np.arange(len(values.categories), dtype=values.codes.dtype)
categories = Categorical.from_codes(cat_codes, dtype=values.dtype)
cat = Categorical.from_codes(cat_codes, dtype=values.dtype)
categories = CategoricalIndex(cat)
codes = values.codes
else:
# The value of ordered is irrelevant since we don't use cat as such,
Expand All @@ -2680,26 +2682,26 @@ def factorize_from_iterable(values):
return codes, categories


def factorize_from_iterables(iterables):
def factorize_from_iterables(iterables) -> Tuple[List[np.ndarray], List[Index]]:
"""
A higher-level wrapper over `factorize_from_iterable`.

*This is an internal function*

Parameters
----------
iterables : list-like of list-likes

Returns
-------
codes_list : list of ndarrays
categories_list : list of Indexes
codes : list of ndarrays
categories : list of Indexes

Notes
-----
See `factorize_from_iterable` for more info.
"""
if len(iterables) == 0:
# For consistency, it should return a list of 2 lists.
return [[], []]
return map(list, zip(*(factorize_from_iterable(it) for it in iterables)))
# For consistency, it should return two empty lists.
return [], []

codes, categories = zip(*(factorize_from_iterable(it) for it in iterables))
return list(codes), list(categories)
2 changes: 1 addition & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ def get_empty_frame(data) -> DataFrame:
if prefix is None:
dummy_cols = levels
else:
dummy_cols = [f"{prefix}{prefix_sep}{level}" for level in levels]
dummy_cols = Index([f"{prefix}{prefix_sep}{level}" for level in levels])

index: Optional[Index]
if isinstance(data, Series):
Expand Down