-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Comments
take |
take |
I found that this seems to be an issue with floats when passing an array of floats to index(): However, when you convert it as Index(bins) in the argument of _bins_to_cut, the values are: |
pandas/pandas/core/reshape/tile.py Lines 339 to 341 in bfe5be0
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. ] |
Thanks to @rob-sil, it seems this issue has been solved in #59409 @mroeschke |
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
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
The text was updated successfully, but these errors were encountered: