Skip to content

fix: fix value_counts column label for normalize=True #245

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

Merged
merged 6 commits into from
Dec 6, 2023
4 changes: 3 additions & 1 deletion bigframes/core/block_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ def value_counts(
)
]
)
return block.select_column(count_id).with_column_labels(["count"])
return block.select_column(count_id).with_column_labels(
["proportion" if normalize else "count"]
)


def pct_change(block: blocks.Block, periods: int = 1) -> blocks.Block:
Expand Down
6 changes: 2 additions & 4 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3453,6 +3453,8 @@ def test_df_to_orc(scalars_df_index, scalars_pandas_df_index):
],
)
def test_df_value_counts(scalars_dfs, subset, normalize, ascending, dropna):
if pd.__version__.startswith("1."):
pytest.skip("pandas 1.x produces different column labels.")
scalars_df, scalars_pandas_df = scalars_dfs

bf_result = (
Expand All @@ -3464,10 +3466,6 @@ def test_df_value_counts(scalars_dfs, subset, normalize, ascending, dropna):
subset, normalize=normalize, ascending=ascending, dropna=dropna
)

# Older pandas version may not have these values, bigframes tries to emulate 2.0+
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, just one one more test test_value_counts_w_cut in test_series.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

pd_result.name = "count"
pd_result.index.names = bf_result.index.names

pd.testing.assert_series_equal(
bf_result, pd_result, check_dtype=False, check_index_type=False
)
Expand Down
11 changes: 4 additions & 7 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,23 +1940,23 @@ def test_cummax_int(scalars_df_index, scalars_pandas_df_index):


def test_value_counts(scalars_dfs):
if pd.__version__.startswith("1."):
pytest.skip("pandas 1.x produces different column labels.")
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "int64_too"

bf_result = scalars_df[col_name].value_counts().to_pandas()
pd_result = scalars_pandas_df[col_name].value_counts()

# Older pandas version may not have these values, bigframes tries to emulate 2.0+
pd_result.name = "count"
pd_result.index.name = col_name

pd.testing.assert_series_equal(
bf_result,
pd_result,
)


def test_value_counts_w_cut(scalars_dfs):
if pd.__version__.startswith("1."):
pytest.skip("value_counts results different in pandas 1.x.")
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "int64_col"

Expand All @@ -1965,9 +1965,6 @@ def test_value_counts_w_cut(scalars_dfs):

bf_result = bf_cut.value_counts().to_pandas()
pd_result = pd_cut.value_counts()
# Older pandas version may not have these values, bigframes tries to emulate 2.0+
pd_result.name = "count"
pd_result.index.name = col_name
pd_result.index = pd_result.index.astype(pd.Int64Dtype())

pd.testing.assert_series_equal(
Expand Down