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 Up Typing in GroupBy #26089

Merged
merged 8 commits into from
Apr 17, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ Other API Changes
- :meth:`Timestamp.strptime` will now rise a ``NotImplementedError`` (:issue:`25016`)
- Comparing :class:`Timestamp` with unsupported objects now returns :py:obj:`NotImplemented` instead of raising ``TypeError``. This implies that unsupported rich comparisons are delegated to the other object, and are now consistent with Python 3 behavior for ``datetime`` objects (:issue:`24011`)
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)
- The ``arg`` argument in :meth:`pandas.core.groupby.DataFrameGroupBy.agg` has been renamed to ``func`` (:issue:`26089`)

.. _whatsnew_0250.deprecations:

Expand Down
9 changes: 0 additions & 9 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ ignore_errors=True
[mypy-pandas.core.config_init]
ignore_errors=True

[mypy-pandas.core.groupby.generic]
ignore_errors=True

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

[mypy-pandas.core.groupby.ops]
ignore_errors=True

[mypy-pandas.core.indexes.base]
ignore_errors=True

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,25 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True,

return new_items, new_blocks

def aggregate(self, arg, *args, **kwargs):
def aggregate(self, func, *args, **kwargs):
Copy link
Member Author

Choose a reason for hiding this comment

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

The reason for this change is that MyPy was complaining about the signature here differening from what's defined in SelectionMixin. This aligns the signatures and is arguably more readable.

Counter argument is that this is an API change, but this actually would match the docs

image

Copy link
Contributor

Choose a reason for hiding this comment

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

this is ok, pls add a note in the whatsnew about this though


_level = kwargs.pop('_level', None)
result, how = self._aggregate(arg, _level=_level, *args, **kwargs)
result, how = self._aggregate(func, _level=_level, *args, **kwargs)
if how is None:
return result

if result is None:

# grouper specific aggregations
if self.grouper.nkeys > 1:
return self._python_agg_general(arg, *args, **kwargs)
return self._python_agg_general(func, *args, **kwargs)
else:

# try to treat as if we are passing a list
try:
assert not args and not kwargs
result = self._aggregate_multiple_funcs(
[arg], _level=_level, _axis=self.axis)
[func], _level=_level, _axis=self.axis)

result.columns = Index(
result.columns.levels[0],
Expand All @@ -174,7 +174,7 @@ def aggregate(self, arg, *args, **kwargs):
# to SparseDataFrame, so we do it here.
result = SparseDataFrame(result._data)
except Exception:
result = self._aggregate_generic(arg, *args, **kwargs)
result = self._aggregate_generic(func, *args, **kwargs)

if not self.as_index:
self._insert_inaxis_grouper_inplace(result)
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class providing the base-class of operations.
import datetime
from functools import partial, wraps
import types
from typing import Optional, Tuple, Type
from typing import FrozenSet, Optional, Tuple, Type
import warnings

import numpy as np

from pandas._config.config import option_context

from pandas._libs import Timestamp, groupby as libgroupby
from pandas._libs import Timestamp
import pandas._libs.groupby as libgroupby
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -325,7 +326,7 @@ def _group_selection_context(groupby):

class _GroupBy(PandasObject, SelectionMixin):
_group_selection = None
_apply_whitelist = frozenset()
_apply_whitelist = frozenset() # type: FrozenSet[str]

def __init__(self, obj, keys=None, axis=0, level=None,
grouper=None, exclusions=None, selection=None, as_index=True,
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import numpy as np

from pandas._libs import NaT, groupby as libgroupby, iNaT, lib, reduction
from pandas._libs import NaT, iNaT, lib
import pandas._libs.groupby as libgroupby
import pandas._libs.reduction as reduction
from pandas.compat import lzip
from pandas.errors import AbstractMethodError
from pandas.util._decorators import cache_readonly
Expand Down