-
-
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
Conversation
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.
Thanks @alexifm for the PR!
Some comments. More generally the second function is a lot to take in, wondering if it can be simplified somehow
yea, probably can just run the groupby on all the columns and remove the parametrization. I'd have to check again if there was anything special about 1 vs multiple fields in a groupby. Or perhaps split into two simpler tests? |
Unless they're direct replicas of each other we likely want to test both
i'd say split into two tests |
|
||
indices = df.groupby(gb_cols).indices | ||
|
||
assert set(target.keys()) == set(indices.keys()) |
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.
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 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.
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.
sorry, tm.assert_assert_numpy_array_equal
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.
Why use set? Is the order not expected to be the same?
Creates tests for GH26859
b0d29d6
to
26e9084
Compare
The difference is whether the keys are tuples or not:
So nearly identical behavior but subtly different. I split the tests but can bring them back together. |
|
||
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 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.
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.
@arw2019 is right, split into two tests instead of stacking two different tests within one. However, when the tests are nearly identical, they should be parametrized instead. I think that's the case here, see my question below.
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 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?
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.
If this can be removed, then I think these tests can be combined using @pytest.mark.parametrize
def test_single_groupby_indices_output(): | ||
cols = [ | ||
"int", | ||
"int_cat", |
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.
|
||
indices = df.groupby(gb_cols).indices | ||
|
||
assert set(target.keys()) == set(indices.keys()) |
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.
Why use set? Is the order not expected to be the same?
@alexifm can you merge master and update to comments |
This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this. |
closing as stale. if you want to continue, pls ping and can re-open. |
Creates tests for GH26859
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Does this warrant an entry in whatsnew?