Skip to content

Remove properties from groupby whitelists #17600

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

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 22 additions & 11 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@

_series_apply_whitelist = ((_common_apply_whitelist |
{'nlargest', 'nsmallest'}) -
{'boxplot'}) | frozenset(['dtype', 'unique'])
{'boxplot'}) | frozenset(['unique'])

_dataframe_apply_whitelist = ((_common_apply_whitelist |
frozenset(['dtypes', 'corrwith'])) -
frozenset(['corrwith'])) -
{'boxplot'})

_cython_transforms = frozenset(['cumprod', 'cumsum', 'shift',
Expand Down Expand Up @@ -2782,13 +2782,6 @@ def _whitelist_method_generator(klass, whitelist):
\"""
f = %(self)s.__getattr__('%(name)s')
return f(%(args)s)"""
property_wrapper_template = \
"""@property
def %(name)s(self) :
\"""
%(doc)s
\"""
return self.__getattr__('%(name)s')"""
for name in whitelist:
# don't override anything that was explicitly defined
# in the base class
Expand All @@ -2811,8 +2804,8 @@ def %(name)s(self) :
'self': args[0],
'args': ','.join(args_by_name)}
else:
wrapper_template = property_wrapper_template
params = {'name': name, 'doc': doc}
msg = 'property %s should be implemented directly.' % name
raise ValueError(msg, type(f))
yield wrapper_template % params


Expand All @@ -2824,6 +2817,14 @@ class SeriesGroupBy(GroupBy):
_series_apply_whitelist):
exec(_def_str)

@property
def dtype(self):
"""
return the dtype object of the underlying data
"""
self._set_group_selection()
return self.apply(lambda x: x.dtype)

@property
def _selection_name(self):
"""
Expand Down Expand Up @@ -3980,6 +3981,16 @@ class DataFrameGroupBy(NDFrameGroupBy):

_block_agg_axis = 1

@property
def dtypes(self):
"""
Return the dtypes in this object.
"""
# need to setup the selection
# as are not passed directly but in the grouper
self._set_group_selection()
return self.apply(lambda x: x.dtypes)

_agg_doc = dedent("""
Examples
--------
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/groupby/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
'plot',
'hist',
'median',
'dtypes',
'corrwith',
'corr',
'cov',
Expand Down Expand Up @@ -81,7 +80,6 @@
'plot',
'hist',
'median',
'dtype',
'corr',
'cov',
'diff',
Expand Down