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

Conversation

alexifm
Copy link

@alexifm alexifm commented Dec 3, 2020

Creates tests for GH26859

Does this warrant an entry in whatsnew?

Copy link
Member

@arw2019 arw2019 left a 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

@alexifm
Copy link
Author

alexifm commented Dec 4, 2020

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?

@arw2019
Copy link
Member

arw2019 commented Dec 4, 2020

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.

Unless they're direct replicas of each other we likely want to test both

Or perhaps split into two simpler tests?

i'd say split into two tests

@jreback jreback added Groupby Testing pandas testing functions or related to the test suite labels Dec 4, 2020

indices = df.groupby(gb_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?

@alexifm
Copy link
Author

alexifm commented Dec 4, 2020

@arw2019

Unless they're direct replicas of each other we likely want to test both

The difference is whether the keys are tuples or not:

>>> df.groupby(['a', 'b']).indices
{('a', Timestamp('2018-01-01 00:00:00')): array([0]),
 ('b', Timestamp('2018-02-01 00:00:00')): array([1]),
 ('c', Timestamp('2018-03-01 00:00:00')): array([2])}

>>> df.groupby(['a']).indices
{'a': array([0]), 'b': array([1]), 'c': array([2])}

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")
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.

Copy link
Member

@rhshadrach rhshadrach left a 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:
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

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.


indices = df.groupby(gb_cols).indices

assert set(target.keys()) == set(indices.keys())
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?

@jreback
Copy link
Contributor

jreback commented Jan 1, 2021

@alexifm can you merge master and update to comments

@github-actions
Copy link
Contributor

github-actions bot commented Feb 1, 2021

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.

@github-actions github-actions bot added the Stale label Feb 1, 2021
@jreback
Copy link
Contributor

jreback commented Feb 11, 2021

closing as stale. if you want to continue, pls ping and can re-open.

@jreback jreback closed this Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Stale Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Groupby indices error with datetime categorical
4 participants