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

feat: Pivot table #6075

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

feat: Pivot table #6075

wants to merge 13 commits into from

Conversation

chipkent
Copy link
Member

Add support for computing pivot tables.

Example:

from deephaven import empty_table

y = empty_table(1000).select(["Row=ii%10", "Col=(int)((ii/10) % 30)", "Sentinel=random()"]).where(
    "Sentinel > 0.3")
ys = y.sum_by(["Row", "Col"])

pvt = pivot(ys, ["Row"], "Col", "Sentinel")
pvt2 = pivot(ys, "Row", "Col", "Sentinel", lambda x: f"Col_{x}")

@chipkent chipkent added this to the 0.37.0 milestone Sep 17, 2024
@chipkent chipkent self-assigned this Sep 17, 2024
@mofojed mofojed self-requested a review September 17, 2024 21:07
@jmao-denver
Copy link
Contributor

@chipkent Have you looked at

?

@chipkent
Copy link
Member Author

I was working from the pivot table query that has been floating around. Someone would need to comment that the java way is preferred before making any changes.

Copy link
Member

@mofojed mofojed left a comment

Choose a reason for hiding this comment

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

Side note that in your example in the PR description, it's missing the import for pivot:

from deephaven.experimental.pivot import pivot

And include an example with multiple Rows selected:

from deephaven import empty_table
from deephaven.experimental.pivot import pivot

y = empty_table(1000).select(["Row=ii%10", "Row2=ii%7", "Col=(int)((ii/10) % 30)", "Sentinel=random()"]).where(
    "Sentinel > 0.3")
ys = y.sum_by(["Row", "Row2", "Col"])

pvt = pivot(ys, ["Row", "Row2"], "Col", "Sentinel")
pvt2 = pivot(ys, ["Row", "Row2"], "Col", "Sentinel", lambda x: f"Col_{x}")

Is there a way to convert to a tree table when we have multiple rows? Such that you can expand/collapse the sections.

Comment on lines +61 to +65
value_to_col_name (Optional[Callable[[Any],str]]): A function that converts a value to a column name.
The function should return a string that is a valid column name.
If None (default), a string representation of the value is used as the column name, with invalid
characters replaced by underscores. The character replacement is not guaranteed to produce unique
column names.
Copy link
Member

Choose a reason for hiding this comment

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

I think confusingly worded... it's called value_to_col_name, and says it converts a "value to a column name", which in your example I would expect to be the sum_by value (e.g. values in the Sentinel column); but it's actually receiving the value from within the column_col.

Comment on lines +48 to +49
def pivot(table: Table, row_cols: Union[str, Sequence[str]], column_col: str, value_col: str,
value_to_col_name: Optional[Callable[[Any], str]] = None) -> Table:
Copy link
Member

Choose a reason for hiding this comment

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

I would assume for true pivot tables, customers will ask to be able to pass in an array for columns and values as well, similar to Excel: https://support.microsoft.com/en-us/office/pivotby-function-de86516a-90ad-4ced-8522-3a25fac389cf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants