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

Add DataFrame.pivot_table. #12015

Merged
merged 5 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 docs/cudf/source/api_docs/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ Reshaping, sorting, transposing
DataFrame.interleave_columns
DataFrame.partition_by_hash
DataFrame.pivot
DataFrame.pivot_table
DataFrame.scatter_by_map
DataFrame.sort_values
DataFrame.sort_index
Expand Down
30 changes: 29 additions & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6424,11 +6424,39 @@ def append(
@_cudf_nvtx_annotate
@copy_docstring(reshape.pivot)
def pivot(self, index, columns, values=None):

return cudf.core.reshape.pivot(
self, index=index, columns=columns, values=values
)

@_cudf_nvtx_annotate
@copy_docstring(reshape.pivot_table)
def pivot_table(
self,
values=None,
index=None,
columns=None,
aggfunc="mean",
fill_value=None,
margins=False,
dropna=None,
margins_name="All",
observed=False,
sort=True,
):
return cudf.core.reshape.pivot_table(
self,
values=values,
index=index,
columns=columns,
aggfunc=aggfunc,
fill_value=fill_value,
margins=margins,
dropna=dropna,
margins_name=margins_name,
observed=observed,
sort=sort,
)

@_cudf_nvtx_annotate
@copy_docstring(reshape.unstack)
def unstack(self, level=-1, fill_value=None):
Expand Down
36 changes: 36 additions & 0 deletions python/cudf/cudf/tests/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,42 @@ def test_pivot_table_simple(data, aggfunc, fill_value):
assert_eq(expected, actual, check_dtype=False)


@pytest.mark.parametrize(
"data",
[
{
"A": ["one", "one", "two", "three"] * 6,
"B": ["A", "B", "C"] * 8,
"C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 4,
"D": np.random.randn(24),
"E": np.random.randn(24),
}
],
)
@pytest.mark.parametrize(
"aggfunc", ["mean", "count", {"D": "sum", "E": "count"}]
)
@pytest.mark.parametrize("fill_value", [0])
def test_dataframe_pivot_table_simple(data, aggfunc, fill_value):
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
pdf = pd.DataFrame(data)
expected = pdf.pivot_table(
values=["D", "E"],
index=["A", "B"],
columns=["C"],
aggfunc=aggfunc,
fill_value=fill_value,
)
cdf = cudf.DataFrame(data)
actual = cdf.pivot_table(
values=["D", "E"],
index=["A", "B"],
columns=["C"],
aggfunc=aggfunc,
fill_value=fill_value,
)
assert_eq(expected, actual, check_dtype=False)


def test_crosstab_simple():
a = np.array(
[
Expand Down