Description
-
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