-
-
Notifications
You must be signed in to change notification settings - Fork 141
Added __getattr__ in DataFrameGroupBy #457
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
@@ -359,3 +359,4 @@ class DataFrameGroupBy(GroupBy): | |||
ascending: bool = ..., | |||
dropna: bool = ..., | |||
) -> Series[float]: ... | |||
def __getattr__(self, name: str) -> Series: ... |
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.
This should be SeriesGroupBy
>>> df = pd.DataFrame(
... {
... 'C': [1, 5, 5, 2, 5, 5],
... 'D': [2.0, 5.0, 8.0, 1.0, 2.0, 9.0],
... }
... )
>>> gb = df.groupby('C').D
>>> gb
<pandas.core.groupby.generic.SeriesGroupBy object at 0x000002E6ED25CA00>
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.
Done sir
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.
This should be SeriesGroupBy
tests/test_frame.py
Outdated
} | ||
) | ||
gb = df.groupby("C").D | ||
check(assert_type(gb, SeriesGroupBy), SeriesGroupBy) |
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 I would prefer a test that does not import SeriesGroupBy
because that is not a documented class.
Borrowing from lines 669, but modified to test the getattr:
df = pd.DataFrame(
data={"col1": [1, 1, 2], "col2": [3, 4, 5], "col3": [0, 1, 0], 0: [-1, -1, -1]}
)
check(assert_type(df.groupby("col1").col3.agg(min), pd.Series), pd.Series)
check(
assert_type(df.groupby("col1").col3.agg([min, max]), pd.DataFrame),
pd.DataFrame,
)
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'll do that sir
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.
Sir I am failing to understand that even if we write tests check(assert_type((df.groupby("C").__getattr__("D")), pd.Series), pd.Series)
like this, how will it change because the return type of __getattr__
will still be SeriesGroupBy
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.
The idea is that you are not testing __getattr__()
directly, but indirectly, because you always use a method in SeriesGroupBy
in real code. The example I gave is calling agg()
on the SeriesGroupBy
result. Both df.groupby("col1").col3
and df.groupby("col1")["col3"]
return a SeriesGroupBy
object.
See the test test_types_groupby_agg
for an example where we use the second form, which is where I copied this from.
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.
sir I read the test_types_groupby_agg
but here when I give the str
to __getattr__()
the error is there about expected series but got seriesgroupby
pushing a commit so you can also 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.
Please implement the test as I wrote above.
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 did try it this way it shows AttributeError: 'SeriesGroupBy' object has no attribute 'col2'
but this error should not be reported
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.
Please follow instructions precisely
@@ -359,3 +359,4 @@ class DataFrameGroupBy(GroupBy): | |||
ascending: bool = ..., | |||
dropna: bool = ..., | |||
) -> Series[float]: ... | |||
def __getattr__(self, name: str) -> Series: ... |
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.
This should be SeriesGroupBy
tests/test_frame.py
Outdated
"col3": [9,8,7,5,6,1] | ||
} | ||
) | ||
check(assert_type(df.groupby("col1").__getattr__("col3"), pd.Series), pd.Series) |
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.
This test should be the same as #457 (comment)
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.
Done
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 @ramvikrams
Thanks sir |
assert_type()
to assert the type of any return valuethe error is saying expected
Series
but gotSeriesGroupBy
, I can't understand itSorry sir forgot to add the correct commit file name.