Skip to content
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

Fix type annotations in pandas.core.resample #26398

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ ignore_errors=True
[mypy-pandas.core.panel]
ignore_errors=True

[mypy-pandas.core.resample]
ignore_errors=True

[mypy-pandas.core.reshape.merge]
ignore_errors=True

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
split-apply-combine paradigm.
"""

from typing import Tuple
import warnings

import numpy as np
Expand Down Expand Up @@ -84,7 +85,8 @@ class Grouper:

>>> df.groupby(Grouper(level='date', freq='60s', axis=1))
"""
_attributes = ('key', 'level', 'freq', 'axis', 'sort')
_attributes = ('key', 'level', 'freq', 'axis',
'sort') # type: Tuple[str, ...]

def __new__(cls, *args, **kwargs):
if kwargs.get('freq') is not None:
Expand Down
22 changes: 12 additions & 10 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
from datetime import timedelta
from textwrap import dedent
from typing import Dict, no_type_check
import warnings

import numpy as np
Expand Down Expand Up @@ -31,7 +32,7 @@
from pandas.tseries.frequencies import to_offset
from pandas.tseries.offsets import DateOffset, Day, Nano, Tick

_shared_docs_kwargs = dict()
_shared_docs_kwargs = dict() # type: Dict[str, str]


class Resampler(_GroupBy):
Expand Down Expand Up @@ -873,25 +874,25 @@ def f(self, _method=method, min_count=0, *args, **kwargs):
for method in ['min', 'max', 'first', 'last', 'mean', 'sem',
'median', 'ohlc']:

def f(self, _method=method, *args, **kwargs):
def g(self, _method=method, *args, **kwargs):
nv.validate_resampler_func(_method, args, kwargs)
return self._downsample(_method)
f.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, f)
g.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, g)

# groupby & aggregate methods
for method in ['count']:
def f(self, _method=method):
def h(self, _method=method):
return self._downsample(_method)
f.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, f)
h.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, h)

# series only methods
for method in ['nunique']:
def f(self, _method=method):
def h(self, _method=method):
return self._downsample(_method)
f.__doc__ = getattr(SeriesGroupBy, method).__doc__
setattr(Resampler, method, f)
h.__doc__ = getattr(SeriesGroupBy, method).__doc__
setattr(Resampler, method, h)


def _maybe_process_deprecations(r, how=None, fill_method=None, limit=None):
Expand Down Expand Up @@ -964,6 +965,7 @@ def __init__(self, obj, *args, **kwargs):
self._groupby.grouper.mutated = True
self.groupby = copy.copy(parent.groupby)

@no_type_check
Copy link
Contributor Author

@gwrome gwrome May 14, 2019

Choose a reason for hiding this comment

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

This file has a set of mismatched signatures that's much more involved to bring into alignment that the ones we did in other PRs.

_upsample

DatetimeIndexResampler and Resampler both define an _upsample method. The two signatures are only marginally different and could be harmonized easily. But _GroupByMixin defines an _apply method that is significantly different and then assigns that method to the class's _upsample variable (as well as _downsample and _groupby_and_aggregate).

_downsample

_downsample has similar relationships between PeriodIndexResampler, DatetimeIndexResampler, and _GroupByMixin`.

_groupby_and_aggregate

_groupby_and_aggregate has similar relationships between Resampler and _GroupByMixin.

Some parts of the _apply signature appear to be unused in this file. I'm investigating how much mucking around I can do with them. It might be possible to bring them all in alignment.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gwrome there is lots of magic to make grouby/resample/rolling play nicely. But can of course do this another time.

def _apply(self, f, grouper=None, *args, **kwargs):
"""
Dispatch to _upsample; we are stripping all of the _upsample kwargs and
Expand Down