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
14 changes: 11 additions & 3 deletions bigframes/core/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,9 +2129,17 @@ def _get_unique_values(
import bigframes.core.block_transforms as block_tf
import bigframes.dataframe as df

unique_value_block = block_tf.drop_duplicates(
self.select_columns(columns), columns
)
if self.explicitly_ordered:
unique_value_block = block_tf.drop_duplicates(
self.select_columns(columns), columns
)
else:
unique_value_block, _ = self.aggregate(by_column_ids=columns, dropna=False)
col_labels = self._get_labels_for_columns(columns)
unique_value_block = unique_value_block.reset_index(
drop=False
).with_column_labels(col_labels)

pd_values = (
df.DataFrame(unique_value_block).head(max_unique_values + 1).to_pandas()
)
Expand Down
4 changes: 0 additions & 4 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3346,8 +3346,6 @@ def _pivot(
)
return DataFrame(pivot_block)

@validations.requires_index
@validations.requires_ordering()
def pivot(
self,
*,
Expand All @@ -3361,8 +3359,6 @@ def pivot(
) -> DataFrame:
return self._pivot(columns=columns, index=index, values=values)

@validations.requires_index
@validations.requires_ordering()
def pivot_table(
self,
values: typing.Optional[
Expand Down
12 changes: 12 additions & 0 deletions tests/system/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ def scalars_df_null_index(
).sort_values("rowindex")


@pytest.fixture(scope="session")
def scalars_df_unordered(
scalars_table_id: str, unordered_session: bigframes.Session
) -> bigframes.dataframe.DataFrame:
"""DataFrame pointing at test data."""
df = unordered_session.read_gbq(
scalars_table_id, index_col=bigframes.enums.DefaultIndexKind.NULL
)
assert not df._block.explicitly_ordered
return df


@pytest.fixture(scope="session")
def scalars_df_2_default_index(
scalars_df_2_index: bigframes.dataframe.DataFrame,
Expand Down
24 changes: 24 additions & 0 deletions tests/system/small/test_unordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,27 @@ def test__resample_with_index(unordered_session, rule, origin, data):
pd.testing.assert_frame_equal(
bf_result, pd_result, check_dtype=False, check_index_type=False
)


@pytest.mark.parametrize(
("values", "index", "columns"),
[
("int64_col", "int64_too", ["string_col"]),
(["int64_col"], "int64_too", ["string_col"]),
(["int64_col", "float64_col"], "int64_too", ["string_col"]),
],
)
def test_unordered_df_pivot(
scalars_df_unordered, scalars_pandas_df_index, values, index, columns
):
bf_result = scalars_df_unordered.pivot(
values=values, index=index, columns=columns
).to_pandas()
pd_result = scalars_pandas_df_index.pivot(
values=values, index=index, columns=columns
)

# Pandas produces NaN, where bq dataframes produces pd.NA
bf_result = bf_result.fillna(float("nan"))
pd_result = pd_result.fillna(float("nan"))
pd.testing.assert_frame_equal(bf_result, pd_result, check_dtype=False)