Skip to content

PERF: Performance regression in Groupby.apply with group_keys=True #53195

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 1 commit into from
May 12, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed performance regression in :meth:`GroupBy.apply` (:issue:`53195`)
- Fixed regression in :func:`read_sql` dropping columns with duplicated column names (:issue:`53117`)
- Fixed regression in :meth:`DataFrame.loc` losing :class:`MultiIndex` name when enlarging object (:issue:`53053`)
- Fixed regression in :meth:`DataFrame.to_string` printing a backslash at the end of the first row of data, instead of headers, when the DataFrame doesn't fit the line width (:issue:`53054`)
Expand Down
24 changes: 14 additions & 10 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def _clean_keys_and_objs(
keys = type(keys).from_tuples(clean_keys, names=keys.names)
else:
name = getattr(keys, "name", None)
keys = Index(clean_keys, name=name)
keys = Index(clean_keys, name=name, dtype=getattr(keys, "dtype", None))

if len(objs) == 0:
raise ValueError("All objects passed were None")
Expand Down Expand Up @@ -806,15 +806,19 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None) -> MultiInde

for hlevel, level in zip(zipped, levels):
to_concat = []
for key, index in zip(hlevel, indexes):
# Find matching codes, include matching nan values as equal.
mask = (isna(level) & isna(key)) | (level == key)
if not mask.any():
raise ValueError(f"Key {key} not in level {level}")
i = np.nonzero(mask)[0][0]

to_concat.append(np.repeat(i, len(index)))
codes_list.append(np.concatenate(to_concat))
if isinstance(hlevel, Index) and hlevel.equals(level):
lens = [len(idx) for idx in indexes]
codes_list.append(np.repeat(np.arange(len(hlevel)), lens))
else:
for key, index in zip(hlevel, indexes):
# Find matching codes, include matching nan values as equal.
mask = (isna(level) & isna(key)) | (level == key)
if not mask.any():
raise ValueError(f"Key {key} not in level {level}")
i = np.nonzero(mask)[0][0]

to_concat.append(np.repeat(i, len(index)))
codes_list.append(np.concatenate(to_concat))

concat_index = _concat_indexes(indexes)

Expand Down