Skip to content

BUG: pandas.groupby.apply does not work with floating point indexed dataframe since 1.0.0 #34675

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
2 of 3 tasks
r-luo opened this issue Jun 9, 2020 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request

Comments

@r-luo
Copy link

r-luo commented Jun 9, 2020

  • 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  # pandas==1.0.4, but this issue exists since 1.0.0
import numpy as np  # numpy==1.16.3

toy_df = pd.DataFrame(
    {
        'group': np.random.randint(0, 5, 10), 
        'value': np.random.random(10)
    }, 
    index=np.random.random(10),
)

# fails with floating point index
toy_df.groupby('group').apply(sum)
toy_df.groupby('group').apply(lambda x: x['value'].sum())

# with integer index then it works:
toy_df.index = np.random.randint(0, 5, 10)
toy_df.groupby('group').apply(lambda x: x['value'].sum())
toy_df.groupby('group').apply(sum)

Problem description

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

I found this peculiar issue with the new version of pandas. When I have floating point index in a pandas DataFrame, then I cannot perform any groupby.apply operation on it. It will always give the below error:

ValueError: index must be monotonic increasing or decreasing
During handling of the above exception, another exception occurred:
...
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Float64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Float64HashTable.get_item()
KeyError: 0.0

When I used ipdb to trace inside the error, I found that the following line caused the problem:

> pandas/core/groupby/ops.py(930)fast_apply()
    929 
--> 930         sdata = self._get_sorted_data()
    931         return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)

after this line, sdata still does not have a sorted index, and then the next line raises the ValueError: index must be monotonic increasing or decreasing.

I tested that this error exists in pandas==1.0.4 and pandas==1.0.0 but does not exist in pandas==0.25.1.

Expected Output

Expected output from groupby apply should be the same in the example with either floating point index or integer index.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.0.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.173-137.229.amzn2.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.4
numpy : 1.16.3
pytz : 2020.1
dateutil : 2.8.0
pip : 20.0.2
setuptools : 47.1.1.post20200604
Cython : 0.29.2
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.15.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.17.1
pytables : None
pytest : None
pyxlsb : None
s3fs : 0.4.0
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@r-luo r-luo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 9, 2020
@rhshadrach
Copy link
Member

Thanks for reporting this. I think this may be a duplicate of #34455.

@r-luo
Copy link
Author

r-luo commented Jun 19, 2020

@rhshadrach Yeah it seems to be the same underlying problem.

@rhshadrach
Copy link
Member

Closing as duplicate of #34455

@rhshadrach rhshadrach added Duplicate Report Duplicate issue or pull request and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants