-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: annotations in core.groupby #35939
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
TYP: annotations in core.groupby #35939
Conversation
pandas/core/groupby/generic.py
Outdated
@@ -1638,7 +1638,7 @@ def _get_data_to_aggregate(self) -> BlockManager: | |||
else: | |||
return obj._mgr | |||
|
|||
def _insert_inaxis_grouper_inplace(self, result): | |||
def _insert_inaxis_grouper_inplace(self, result: 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.
for functions other than __init__ you can add None as the return type.
@@ -734,7 +734,7 @@ def pipe(self, func, *args, **kwargs): | |||
|
|||
plot = property(GroupByPlot) | |||
|
|||
def _make_wrapper(self, name): | |||
def _make_wrapper(self, name: str) -> Callable: |
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 type parameters for Callable (if you can)
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.
its going to end up being wrapper(self, *args, **kwargs) -> FrameOrSeriesUnion
. Is that Callable[..., FrameOrSeriesUnion]
?
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.
sure
pandas\core\groupby\categorical.py:101: error: "CategoricalIndex" has no attribute "set_categories"; maybe "categories"? [attr-defined] These are because mypy is a static type checker and these methods are dynamically added to CategoricalIndex in I'm OK with # type: ignores on this pass (with error code) |
error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[object], Iterator[_T]]" [arg-type] not sure on this one. mypy is happy with
which I think is easier to grok anyhow. |
see python/mypy#6811 (comment)
|
pandas/core/groupby/generic.py
Outdated
@@ -1848,6 +1848,7 @@ def nunique(self, dropna: bool = True): | |||
], | |||
axis=1, | |||
) | |||
assert isinstance(results, DataFrame) # for mypy |
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.
if you use cast here, then once we adopt Literal and concat can be overloaded on axis value, then with warn_redundant_casts
we can remove
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.
neat, will do
group_idx = self.group_index | ||
assert isinstance(group_idx, CategoricalIndex) # set in __init__ | ||
return recode_from_groupby(self.all_grouper, self.sort, group_idx) |
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.
is this needed.
are the type annotations for def group_index(self) -> Index
and _group_index: Optional[Index] = None
correct.
in __init__
self._group_index = CategoricalIndex(..
or
(
self.grouper,
self._codes,
self._group_index,
) = index._get_grouper_for_level(self.grouper, level)
so self._group_index can only be CategoricalIndex or None and group_index can only be CategoricalIndex ?
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.
IIRC its the self.all_grouper check a few lines up that ensures we have a CategoricalIndex here
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.
self.all_grouper is not None when is_categorical_dtype(self.grouper) so I don't think that narrows it. (but I may need to trace through further)
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 @jbrockmendel generally lgtm.
Thanks @jbrockmendel |
I'm still seeing a couple of mypy complaints, suggestions @simonjayhawkins ?