Skip to content

BUG: DataError: No numeric types to aggregate on pandas.core.groupby.GroupBy.rank¶ #38278

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
3 tasks done
ppetruneac opened this issue Dec 3, 2020 · 2 comments · Fixed by #41498
Closed
3 tasks done
Milestone

Comments

@ppetruneac
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

  import pandas as pd
  df =  pd.DataFrame({'id': ['1', '1', '2', '3'], 'title': ['title1', 'title2', 'title3', 'title4']})
  # df['id'] = df['id'].astype('int')
  # df.dtypes  
  df.groupby('id')['title'].rank(method = "first")

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

For a simple dataframe, I cannot rank a grouped dataframe on non-numeric data type. For example, I want to group by ID and rank a column. My aim here is to identify duplicates in one column by ranking it.

This is the problem I get:

df.groupby('id')['title'].rank(method = "first")
---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
~/projects/dentaway-marketing/linkedin-jobs-scraper/process_linkedin_jobs_data.py in 
----> 276 df.groupby('id')['title'].rank(method = "first")

~/anaconda3/envs/dev/lib/python3.8/site-packages/pandas/core/groupby/groupby.py in rank(self, method, ascending, na_option, pct, axis)
   2299             msg = "na_option must be one of 'keep', 'top', or 'bottom'"
   2300             raise ValueError(msg)
-> 2301         return self._cython_transform(
   2302             "rank",
   2303             numeric_only=False,

~/anaconda3/envs/dev/lib/python3.8/site-packages/pandas/core/groupby/groupby.py in _cython_transform(self, how, numeric_only, **kwargs)
    970 
    971         if len(output) == 0:
--> 972             raise DataError("No numeric types to aggregate")
    973 
    974         return self._wrap_transformed_output(output)

DataError: No numeric types to aggregate

Expected Output

The expected output should be a ranked series.

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

pandas is installed with pip.

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 20.1.0
Version : Darwin Kernel Version 20.1.0: Sat Oct 31 00:07:11 PDT 2020; root:xnu-7195.50.7~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.1.4
numpy : 1.18.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0.post20200714
Cython : 0.29.21
pytest : 5.4.3
hypothesis : None
sphinx : 3.1.2
blosc : None
feather : None
xlsxwriter : 1.2.9
lxml.etree : 4.5.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fsspec : 0.7.4
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 2.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.0
sqlalchemy : 1.3.18
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.50.1

@ppetruneac ppetruneac added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 3, 2020
@GYHHAHA
Copy link
Contributor

GYHHAHA commented Dec 4, 2020

The cythonized group_rank seems only to work on numeric columns, not consistent with the Series.rank

def group_rank(float64_t[:, :] out,

@rhshadrach
Copy link
Member

Thanks for the report, it seems like this should be supported. PRs are most certainly welcome!

@rhshadrach rhshadrach added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 4, 2020
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Dec 4, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants