Skip to content
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

perf(postprocessing): improve pivot postprocessing operation #23465

Merged
merged 1 commit into from
Mar 24, 2023
Merged
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
7 changes: 2 additions & 5 deletions superset/utils/pandas_postprocessing/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def pivot( # pylint: disable=too-many-arguments,too-many-locals
if not drop_missing_columns and columns:
for row in df[columns].itertuples():
for metric in aggfunc.keys():
series_set.add(str(tuple([metric]) + tuple(row[1:])))
series_set.add(tuple([metric]) + tuple(row[1:]))
Copy link
Member

Choose a reason for hiding this comment

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

@zhaoyongjie do you have any reservations about this change? IMO we have pretty good test coverage, so if this change doesn't break existing tests I feel this change is good to go (if it does cause a regression it's just a sign that we needed more/better tests). So I feel like this change is warranted, as it simplifies the code.

Copy link
Member

Choose a reason for hiding this comment

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

@villebro from the line of code seems to serialize the combination of tuples, this change(L104) has used vector difference on the columns so I think this PR is better than before performance, and Thanks @Usiel for this PR.


df = df.pivot_table(
values=aggfunc.keys(),
Expand All @@ -101,10 +101,7 @@ def pivot( # pylint: disable=too-many-arguments,too-many-locals
)

if not drop_missing_columns and len(series_set) > 0 and not df.empty:
for col in df.columns:
series = str(col)
if series not in series_set:
df = df.drop(col, axis=PandasAxis.COLUMN)
df = df.drop(df.columns.difference(series_set), axis=PandasAxis.COLUMN)

if combine_value_with_metric:
df = df.stack(0).unstack()
Expand Down