Skip to content

test groupby.indices for multiple groupby and mix of types #38273

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

Closed
wants to merge 1 commit into from
Closed
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
104 changes: 104 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64_any_dtype

import pandas as pd
from pandas import (
DataFrame,
Expand Down Expand Up @@ -361,6 +363,108 @@ def f3(x):
df2.groupby("a").apply(f3)


def test_single_groupby_indices_output():
cols = [
"int",
"int_cat",
Copy link
Member

Choose a reason for hiding this comment

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

Replace this with data dictionary instead, use list(data.keys()) where cols is used now.

"float",
"float_cat",
"dt",
"dt_cat",
"dttz",
"dttz_cat",
"period",
"period_cat",
]

int_ = Series([1, 2, 3])
dt_ = pd.to_datetime(["2018Q1", "2018Q2", "2018Q3"])
dttz_ = dt_.tz_localize("Europe/Berlin")
df = DataFrame(
data={
"int": int_,
"int_cat": int_.astype("category"),
"float": int_.astype("float"),
"float_cat": int_.astype("float").astype("category"),
"dt": dt_,
"dt_cat": dt_.astype("category"),
"dttz": dttz_,
"dttz_cat": dttz_.astype("category"),
"period": dt_.to_period("Q"),
"period_cat": dt_.to_period("Q").astype("category"),
},
columns=cols,
)
for col in df.columns:
col_vals = list(df[col].unique())

if is_datetime64_any_dtype(df[col]):
col_vals = [Timestamp(el) for el in col_vals]

target = {key: np.array([i]) for i, key in enumerate(col_vals)}

indices = df.groupby(col).indices

assert set(target.keys()) == set(indices.keys())
for key in target.keys():
assert pd.core.dtypes.missing.array_equivalent(target[key], indices[key])


def test_multiple_groupby_indices_output():
cols = [
"int",
"int_cat",
"float",
"float_cat",
"dt",
"dt_cat",
"dttz",
"dttz_cat",
"period",
"period_cat",
"value",
]

int_ = Series([1, 2, 3])
dt_ = pd.to_datetime(["2018Q1", "2018Q2", "2018Q3"])
dttz_ = dt_.tz_localize("Europe/Berlin")
Copy link
Contributor

Choose a reason for hiding this comment

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

these test are overly complicated. can you simplify this by hard-coding the expected index locations and just doing a single comparision at the end.

df = DataFrame(
data={
"int": int_,
"int_cat": int_.astype("category"),
"float": int_.astype("float"),
"float_cat": int_.astype("float").astype("category"),
"dt": dt_,
"dt_cat": dt_.astype("category"),
"dttz": dttz_,
"dttz_cat": dttz_.astype("category"),
"period": dt_.to_period("Q"),
"period_cat": dt_.to_period("Q").astype("category"),
"value": Series([1, 2, 3]),
},
columns=cols,
)
groupby_cols = cols[:-1]
col_vals = {col: list(df[col].unique()) for col in groupby_cols}

for col in groupby_cols:
is_dt = is_datetime64_any_dtype(df[col])
is_cat_dt = is_categorical_dtype(df[col]) and is_datetime64_any_dtype(
df[col].cat.categories
)
if is_dt or is_cat_dt:
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 is_cat_dt can be removed here, can you check?

Copy link
Member

@rhshadrach rhshadrach Dec 20, 2020

Choose a reason for hiding this comment

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

If this can be removed, then I think these tests can be combined using @pytest.mark.parametrize

col_vals[col] = [Timestamp(el) for el in col_vals[col]]

it = zip(*(col_vals[col] for col in groupby_cols))
target = {key: np.array([i]) for i, key in enumerate(it)}

indices = df.groupby(groupby_cols).indices

assert set(target.keys()) == set(indices.keys())
Copy link
Contributor

Choose a reason for hiding this comment

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

use self.assert_numpy_array_equal for the comparisions.

pls simply as much as possible.

Copy link
Author

Choose a reason for hiding this comment

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

I don't follow. What is self in this? Also that's a comparison on the keys, which are single objects or tuples, depending on how many columns are in the groupby.

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, tm.assert_assert_numpy_array_equal

Copy link
Member

Choose a reason for hiding this comment

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

Why use set? Is the order not expected to be the same?

for key in target.keys():
assert pd.core.dtypes.missing.array_equivalent(target[key], indices[key])


def test_attr_wrapper(ts):
grouped = ts.groupby(lambda x: x.weekday())

Expand Down