-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Added repr string for Grouper and TimeGrouper #18203
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
pandas/core/groupby.py
Outdated
@@ -233,6 +233,8 @@ class Grouper(object): | |||
|
|||
>>> df.groupby(Grouper(level='date', freq='60s', axis=1)) | |||
""" | |||
_attributes = (('key', None), ('level', None), ('freq', None), |
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.
again you don't need to specify the default, just list the attributes. The repr should include all the values, not just the defaults. Further this means you are defining defaults in 2 places, here and the init, not a good idea.
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.
Ok, changed. Also added test for TimeGrouper.__repr__
. Not sure where a test for Grouper.__repr__
should go.
Should a note about this go in the 0.21.1 or 0.22 whatsnew.rst?
eb6bc4c
to
deab64f
Compare
Hello @topper-123! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on November 27, 2017 at 01:12 Hours UTC |
a603bee
to
df67063
Compare
After changes according to @jreback, the output is now: >>> pd.Grouper(key='A')
Grouper(key='A', level=None, freq=None, axis=0, sort=False)
>>> pd.Grouper(key='A', freq='H')
TimeGrouper(key='A', level=None, freq=<Hour>, sort=True, closed='left', label='left', how='mean', nperiods=None, axis=0, fill_method=None, limit=None, loffset=None, kind=None, convention='e', base=0) |
so what you can do is skip printing attributes that are None |
pandas/core/groupby.py
Outdated
@@ -333,6 +334,15 @@ def _set_grouper(self, obj, sort=False): | |||
def groups(self): | |||
return self.grouper.groups | |||
|
|||
def __repr__(self): | |||
attrs_list = [] | |||
for attr_name in self._attributes: |
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.
can be a list comprehension
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.
Ok.
pandas/core/resample.py
Outdated
@@ -1025,6 +1025,9 @@ class TimeGrouper(Grouper): | |||
Use begin, end, nperiods to generate intervals that cannot be derived | |||
directly from the associated object | |||
""" | |||
_attributes = ('key', 'level', 'freq', 'sort', 'closed', 'label', | |||
'how', 'nperiods', 'axis', 'fill_method', 'limit', |
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.
only should have public attributes
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.
iow you don’t need any special ones for TimeGrouper (which is publicly deprecated anyhow)
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.
A TimeGrouper
object is returned when calling Grouper
with a freq
arg, IOW it's only the top level location of TimeGrouper
that is deprecated, right?
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.
yes however you will never actually directly construct one
point being is the args are the same as Grouper (maybe a couple of extra which are prob not documented)
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.
can u add the missing arguments in the doc string of Grouper? eg convention, base, loffset i think are only public ones
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.
Ah of course. Changed.
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.
Considering your last comment, it's actually possible to to:
>>> pd.Grouper(key='A', freq='H', convention='s')
TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True)
Note the output now misses convention='s'
, even though it's been set.
Shouldn't TimeGrouper
actually have its own _attributes
to account for this issue?
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.
yeah it should take a copy of the super (Grouper) and add to it
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.
Added _attributes
to TimeGrouper
.
I don't know what base
, loffset
and convention
do, and can't quite figure it out from the code, so can't add explanations to the doc string, sorry. I added them, but without explanations. Do you have a proposal explanation for each?
3add483
to
74aa59d
Compare
pandas/tests/test_resample.py
Outdated
expected = ("TimeGrouper(key='A', freq=<Hour>, sort=True, " | ||
"closed='left', label='left', how='mean', axis=0, " | ||
"convention='e', base=0)") | ||
assert result == expected |
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.
a better test is to actually construct the object and compare to the original
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 understand you here. Do you mean compare the repr of Grouper
and TimeGrouper
?
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.
no, use the repr to actually construct a new object (that's the point of a repr), and compare to the original
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 still don't understand. Should the expected
be wrapped in a repr()
?
74aa59d
to
bf6eb67
Compare
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.
see my other comments
doc/source/whatsnew/v0.21.1.txt
Outdated
@@ -22,7 +22,7 @@ Other Enhancements | |||
^^^^^^^^^^^^^^^^^^ | |||
|
|||
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`) | |||
- | |||
- :class:`Grouper` and :class:`TimeGrouper` now have a friendly repr output. |
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.
issue number
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.
Ok, fixed.
bf6eb67
to
e4b1d5e
Compare
can you rebase |
e4b1d5e
to
833883f
Compare
Codecov Report
@@ Coverage Diff @@
## master #18203 +/- ##
=========================================
Coverage ? 91.38%
=========================================
Files ? 163
Lines ? 50066
Branches ? 0
=========================================
Hits ? 45753
Misses ? 4313
Partials ? 0
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #18203 +/- ##
==========================================
- Coverage 91.35% 91.33% -0.02%
==========================================
Files 163 163
Lines 49796 49797 +1
==========================================
- Hits 45490 45482 -8
- Misses 4306 4315 +9
Continue to review full report at Codecov.
|
pandas/core/resample.py
Outdated
@@ -1025,7 +1025,10 @@ class TimeGrouper(Grouper): | |||
Use begin, end, nperiods to generate intervals that cannot be derived | |||
directly from the associated object | |||
""" | |||
|
|||
_attributes = Grouper._attributes + ('closed', 'label', 'how', | |||
'nperiods', 'fill_method', 'limit', |
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.
can you leave out nperiods
, also fill_method
and limit
which are deprecated.
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.
you can actually completely remove nperiods
as this is non-functional.
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.
lgtm. some comments.
pandas/tests/test_resample.py
Outdated
@@ -3416,3 +3416,10 @@ def test_aggregate_with_nat(self): | |||
|
|||
# if NaT is included, 'var', 'std', 'mean', 'first','last' | |||
# and 'nth' doesn't work yet | |||
|
|||
def test_repr(self): | |||
result = repr(TimeGrouper(key='A', freq='H')) |
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.
add the issue number
pandas/tests/test_resample.py
Outdated
def test_repr(self): | ||
result = repr(TimeGrouper(key='A', freq='H')) | ||
expected = ("TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, " | ||
"closed='left', label='left', how='mean', " |
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.
can you add a similar test for Grouper (in test_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.
added
833883f
to
1a4747b
Compare
green. |
linting issue. otherwise lgtm. note that you can do
|
512c8e2
to
1d4e3d9
Compare
pandas/core/resample.py
Outdated
@@ -1025,6 +1025,10 @@ class TimeGrouper(Grouper): | |||
Use begin, end, nperiods to generate intervals that cannot be derived | |||
directly from the associated object | |||
""" | |||
_attributes = Grouper._attributes + ('closed', 'label', 'how', | |||
'nperiods', 'fill_method', 'limit', |
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.
remove nperiods, fill_method, and limit
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.
and remove nperiods from the signature entirely, its is not used
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.
Ok, but what's the benefit of not having fill_method and limit in _attributes?
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.
because they are deprecated (see the maybe_process_deprecations)
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.
Also, there's this
Notes
-----
Use begin, end, nperiods to generate intervals that cannot be derived
directly from the associated object
None of begin or end seem to exist, and if nperiods isn't used, I've just removed that note
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.
yeah remove that, looks bogus
1d4e3d9
to
bdc5ac2
Compare
All green, @jreback |
thanks! |
(cherry picked from commit f7c79be)
(cherry picked from commit f7c79be)
git diff upstream/master -u -- "*.py" | flake8 --diff
This is a new implementation of #17727. This is much simpler than the previous attempts. The outputs are now:
Tests still need to be written. I can do them when/if this is ok.