Skip to content
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
5 changes: 2 additions & 3 deletions bigframes/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ def crosstab(
columns=tmp_col_names,
aggfunc=aggfunc or "count",
sort=False,
fill_value=0 if (aggfunc is None) else None,
)
# Undo temporary unique level labels
pivot_table.index.names = rownames or [i.name for i in index]
pivot_table.columns.names = colnames or [c.name for c in columns]
if aggfunc is None:
# TODO: Push this into pivot_table itself
pivot_table = pivot_table.fillna(0)
return pivot_table


Expand Down
8 changes: 3 additions & 5 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3486,10 +3486,6 @@ def pivot_table(
observed: bool = False,
sort: bool = True,
) -> DataFrame:
if fill_value is not None:
raise NotImplementedError(
"DataFrame.pivot_table fill_value arg not supported. {constants.FEEDBACK_LINK}"
)
if margins:
raise NotImplementedError(
"DataFrame.pivot_table margins arg not supported. {constants.FEEDBACK_LINK}"
Expand Down Expand Up @@ -3549,14 +3545,16 @@ def pivot_table(
index=index,
values=values if len(values) > 1 else None,
)
if fill_value is not None:
pivoted = pivoted.fillna(fill_value)
if sort:
pivoted = pivoted.sort_index()

# TODO: Remove the reordering step once the issue is resolved.
# The pivot_table method results in multi-index columns that are always ordered.
# However, the order of the pivoted result columns is not guaranteed to be sorted.
# Sort and reorder.
return pivoted[pivoted.columns.sort_values()]
return pivoted.sort_index(axis=1) # type: ignore

def stack(self, level: LevelsType = -1):
if not isinstance(self.columns, pandas.MultiIndex):
Expand Down
29 changes: 22 additions & 7 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3784,12 +3784,18 @@ def test_df_pivot_hockey(hockey_df, hockey_pandas_df, values, index, columns):


@pytest.mark.parametrize(
("values", "index", "columns", "aggfunc"),
("values", "index", "columns", "aggfunc", "fill_value"),
[
(("culmen_length_mm", "body_mass_g"), "species", "sex", "std"),
(["body_mass_g", "culmen_length_mm"], ("species", "island"), "sex", "sum"),
("body_mass_g", "sex", ["island", "species"], "mean"),
("culmen_depth_mm", "island", "species", "max"),
(("culmen_length_mm", "body_mass_g"), "species", "sex", "std", 1.0),
(
["body_mass_g", "culmen_length_mm"],
("species", "island"),
"sex",
"sum",
None,
),
("body_mass_g", "sex", ["island", "species"], "mean", None),
("culmen_depth_mm", "island", "species", "max", -1),
],
)
def test_df_pivot_table(
Expand All @@ -3799,12 +3805,21 @@ def test_df_pivot_table(
index,
columns,
aggfunc,
fill_value,
):
bf_result = penguins_df_default_index.pivot_table(
values=values, index=index, columns=columns, aggfunc=aggfunc
values=values,
index=index,
columns=columns,
aggfunc=aggfunc,
fill_value=fill_value,
).to_pandas()
pd_result = penguins_pandas_df_default_index.pivot_table(
values=values, index=index, columns=columns, aggfunc=aggfunc
values=values,
index=index,
columns=columns,
aggfunc=aggfunc,
fill_value=fill_value,
)
pd.testing.assert_frame_equal(
bf_result, pd_result, check_dtype=False, check_column_type=False
Expand Down
4 changes: 4 additions & 0 deletions third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6414,6 +6414,10 @@ def pivot_table(self, values=None, index=None, columns=None, aggfunc="mean"):
aggfunc (str, default "mean"):
Aggregation function name to compute summary statistics (e.g., 'sum', 'mean').

fill_value (scalar, default None):
Value to replace missing values with (in the resulting pivot table, after
aggregation).

Returns:
bigframes.pandas.DataFrame: An Excel style pivot table.
"""
Expand Down