-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -361,6 +363,108 @@ def f3(x): | |
df2.groupby("a").apply(f3) | ||
|
||
|
||
def test_single_groupby_indices_output(): | ||
cols = [ | ||
"int", | ||
"int_cat", | ||
"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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think is_cat_dt can be removed here, can you check? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow. What is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, tm.assert_assert_numpy_array_equal There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
|
||
|
There was a problem hiding this comment.
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.