Skip to content

CLN: pd.TimeGrouper #26477

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

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed ``Panel`` (:issue:`25047`, :issue:`25191`, :issue:`25231`)
-
-
- Removed previously deprecated ``TimeGrouper`` (:issue:`16942`)
-

.. _whatsnew_0250.performance:
Expand Down
2 changes: 1 addition & 1 deletion pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
to_numeric, to_datetime, to_timedelta,

# misc
np, TimeGrouper, Grouper, factorize, unique, value_counts,
np, Grouper, factorize, unique, value_counts,
array, Categorical, set_eng_float_format, Series, DataFrame,
Panel)

Expand Down
12 changes: 0 additions & 12 deletions pandas/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,3 @@
from pandas.tseries.offsets import DateOffset
from pandas.core.tools.datetimes import to_datetime
from pandas.core.tools.timedeltas import to_timedelta


# Deprecation: xref gh-16747
class TimeGrouper:

def __new__(cls, *args, **kwargs):
from pandas.core.resample import TimeGrouper
import warnings
warnings.warn("pd.TimeGrouper is deprecated and will be removed; "
"Please use pd.Grouper(freq=...)",
FutureWarning, stacklevel=2)
return TimeGrouper(*args, **kwargs)
13 changes: 1 addition & 12 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TestPDApi(Base):
]

# these are already deprecated; awaiting removal
deprecated_classes = ['TimeGrouper', 'Panel']
deprecated_classes = ['Panel']

# these should be deprecated in the future
deprecated_classes_in_future = []
Expand Down Expand Up @@ -132,17 +132,6 @@ def test_testing(self):
self.check(testing, self.funcs)


class TestTopLevelDeprecations:

# top-level API deprecations
# GH 13790

def test_TimeGrouper(self):
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
pd.TimeGrouper(freq='D')


class TestCDateRange:

def test_deprecation_cdaterange(self):
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range
from pandas.core.groupby.ops import BinGrouper
from pandas.core.resample import TimeGrouper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we remove this entirely from resample as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally, TimeGrouper still holds a lot of the core metadata for resampling. The reason why the TimeGrouper isn't needed at the toplevel is because of this shortcut in Grouper:

    def __new__(cls, *args, **kwargs):
        if kwargs.get('freq') is not None:
            from pandas.core.resample import TimeGrouper
            cls = TimeGrouper
        return super().__new__(cls)

So AFAICT, the internal TimeGrouper is still needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comments below, we don't want to use this in the user facing tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any objection to replacing TimeGrouper internally as well? Always looking to reduce GroupBy complexity so getting rid of an entire class would be helpful

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this would be easy

TimeGrouper does a lot of stuff

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured as such. Would still be nice if not for a full class removal to even remove any now internally unused methods and keep paring down the groupby code. If I see any opportunities I'll push a PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely +1 for having Grouper adopt TimeGrouper code. Should be possible.

from pandas.util import testing as tm
from pandas.util.testing import assert_frame_equal, assert_series_equal

Expand Down Expand Up @@ -365,10 +366,8 @@ def sumfunc_value(x):
return x.value.sum()

expected = df.groupby(pd.Grouper(key='date')).apply(sumfunc_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change these to use Grouper (here and below)

with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
result = (df_dt.groupby(pd.TimeGrouper(freq='M', key='date'))
.apply(sumfunc_value))
result = (df_dt.groupby(TimeGrouper(freq='M', key='date'))
.apply(sumfunc_value))
assert_series_equal(result.reset_index(drop=True),
expected.reset_index(drop=True))

Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@


def test_apply():
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
grouper = pd.TimeGrouper(freq='A', label='right', closed='right')
grouper = TimeGrouper(freq='A', label='right', closed='right')

grouped = test_series.groupby(grouper)

Expand All @@ -38,9 +36,7 @@ def test_count():

expected = test_series.groupby(lambda x: x.year).count()

with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
grouper = pd.TimeGrouper(freq='A', label='right', closed='right')
grouper = TimeGrouper(freq='A', label='right', closed='right')
result = test_series.groupby(grouper).count()
expected.index = result.index
assert_series_equal(result, expected)
Expand Down