Skip to content
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

BUG: qcut returns incorrect results #58240

Open
3 tasks done
YerdnY opened this issue Apr 12, 2024 · 4 comments
Open
3 tasks done

BUG: qcut returns incorrect results #58240

YerdnY opened this issue Apr 12, 2024 · 4 comments
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@YerdnY
Copy link

YerdnY commented Apr 12, 2024

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
  
df = pd.DataFrame(np.arange(1, 182), columns=['val'])
pd.qcut(df.val, 10, labels=False).value_counts()

Issue Description

The code above produces the following output:
val
0 19
7 19
1 18
2 18
3 18
4 18
5 18
8 18
9 18
6 17
Name: count, dtype: int64
The issue is there are three unique counts of items in bins - 17, 18, 19. I expect no more then two unique counts. Ideally one, but that is only possible if input size is divisible by nbins.

Expected Behavior

The same code produces this, correct output in pandas 2.1.4:

val
0 19
1 18
2 18
3 18
4 18
5 18
6 18
7 18
8 18
9 18
Name: count, dtype: int64

Installed Versions

C:\ProgramData\anaconda3\envs\quant2\Lib\site-packages_distutils_hack_init_.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit : bdc79c1
python : 3.11.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : English_United States.1252

pandas : 2.2.1
numpy : 1.26.4
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.1
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.4
sqlalchemy : 2.0.25
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@YerdnY YerdnY added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2024
@kymcglyn
Copy link

take

@ashorkey-umich
Copy link

take

@Nrezhang
Copy link
Contributor

I found that this seems to be an issue with floats when passing an array of floats to index():
these are the bins in the function qcut,
0.0 1.0
0.1 19.0
0.2 37.0
0.3 55.0
0.4 73.0
0.5 91.0
0.6 109.0
0.7 127.0
0.8 145.0
0.9 163.0
1.0 181.0

However, when you convert it as Index(bins) in the argument of _bins_to_cut, the values are:
Index([1.0, 19.0, 37.0, 55.00000000000001, 73.0, 91.0, 109.00000000000001, 126.99999999999999, 145.0, 163.0, 181.0]
and the issue is at 126.9999999. Any suggestions for how to resolve this

@yuanx749
Copy link
Contributor

quantiles = np.linspace(0, 1, q + 1) if is_integer(q) else q
bins = x_idx.to_series().dropna().quantile(quantiles)

It is caused by the floating point of np.linspace in qcut.

quantiles = np.linspace(0, 1, 11)
with np.printoptions(precision=20):
    print(quantiles)

Note that the output is actually:

[0.                  0.1                 0.2
 0.30000000000000004 0.4                 0.5
 0.6000000000000001  0.7000000000000001  0.8
 0.9                 1.                 ]

@kymcglyn kymcglyn removed their assignment May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

5 participants