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: std using numpy.float32 dtype gives incorrect result on contant array. #57505

Closed
3 tasks done
jamespreed opened this issue Feb 19, 2024 · 16 comments
Closed
3 tasks done
Labels
Bug Reduction Operations sum, mean, min, max, etc. Upstream issue Issue related to pandas dependency

Comments

@jamespreed
Copy link

jamespreed commented Feb 19, 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

# this should be 0 or very close to 0
print(pd.Series([271.46] * 150000, dtype='float32').std())
# returns: 0.229433074593544

# same result using numpy dtype directly (as you would expect)
print(pd.Series([271.46] * 150000, dtype=np.float32).std())
# returns: 0.229433074593544


# the pandas float32 dtype does not have this issue.
print(pd.Series([271.46] * 150000, dtype=pd.Float32Dtype()).std())
# returns: 0.0

# neither does using the numpy standard deviation function on the numpy values
print(np.std(pd.Series([271.46] * 150000, dtype=np.float32).values))
# returns: 0.0

Issue Description

When using the numpy float32 dtype by passing either the string 'float32' or the numpy dtype np.float32, the value of the standard deviation is incorrect for a constant array (array of identical values). It should return zero, but instead returns a value.

Switching to using the Pandas float32 dtype alleviates the error, as does using np.std on the numpy values of the series.

Expected Behavior

The expected behavior is the following evaluating to True

pd.Series([271.46] * 150000, dtype='float32').std() == 0.0

Installed Versions

INSTALLED VERSIONS
------------------
commit                : f538741432edf55c6b9fb5d0d496d2dd1d7c2457
python                : 3.11.7.final.0
python-bits           : 64
OS                    : Windows
OS-release            : 10
Version               : 10.0.22621
machine               : AMD64
processor             : Intel64 Family 6 Model 151 Stepping 5, GenuineIntel
byteorder             : little
LC_ALL                : None
LANG                  : None
LOCALE                : English_United States.1252

pandas                : 2.2.0
numpy                 : 1.26.3
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            : None
lxml.etree            : 4.9.3
html5lib              : None
pymysql               : None
psycopg2              : None
jinja2                : None
IPython               : 8.15.0
pandas_datareader     : None
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
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            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
zstandard             : None
tzdata                : 2023.3
qtpy                  : None
pyqt5                 : None
@jamespreed jamespreed added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 19, 2024
@Groni3000
Copy link

Groni3000 commented Feb 19, 2024

Have no problems:
image

OS: Windows 10
Python: 3.11.4
Working in venv with such dependencies:

contourpy==1.2.0
cycler==0.12.1
fonttools==4.49.0
kiwisolver==1.4.5
matplotlib==3.8.3
numpy==1.26.4
packaging==23.2
pandas==2.2.0
pillow==10.2.0
pyarrow==15.0.0
pyparsing==3.1.1
python-dateutil==2.8.2
pytz==2024.1
seaborn==0.13.2
six==1.16.0
tzdata==2024.1

Sorry, don't know how to make such "Installed Versions".

Tried the same code with 1.26.3 numpy version, still no problems.

@jamespreed
Copy link
Author

jamespreed commented Feb 19, 2024

image

Updated numpy to 1.26.4 with the same result. Is this a "just on my machine" bug?

@jamespreed
Copy link
Author

Sorry, don't know how to make such "Installed Versions".

You can use:

pd.show_versions()

@Groni3000
Copy link

@jamespreed Thank you very much! Here is my installed versions:

INSTALLED VERSIONS
------------------
commit                : f538741432edf55c6b9fb5d0d496d2dd1d7c2457
python                : 3.11.4.final.0
python-bits           : 64
OS                    : Windows
OS-release            : 10
Version               : 10.0.19045
machine               : AMD64
processor             : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder             : little
LC_ALL                : None
LANG                  : en_US.UTF-8
LOCALE                : Russian_Ukraine.1251

pandas                : 2.2.0
numpy                 : 1.26.4
pytz                  : 2024.1
dateutil              : 2.8.2
setuptools            : 65.5.0
pip                   : 24.0
Cython                : None
pytest                : 8.0.1
hypothesis            : None
sphinx                : None
blosc                 : None
feather               : None
xlsxwriter            : None
lxml.etree            : None
html5lib              : None
pymysql               : None
psycopg2              : None
jinja2                : None
IPython               : None
pandas_datareader     : None
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : None
gcsfs                 : None
matplotlib            : 3.8.3
numba                 : None
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
pyarrow               : 15.0.0
pyreadstat            : None
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : None
sqlalchemy            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
zstandard             : None
tzdata                : 2024.1
qtpy                  : None
pyqt5                 : None

@Groni3000
Copy link

@jamespreed Just out of curious, can you try to create venv and reproduce this bug?
Make sure you are using same python version as described in bug report.

python -m venv PROJECT_FOLDER & cd PROJECT_FOLDER & .\Scripts\activate & pip install numpy pandas

Create your file and try again.

@jamespreed
Copy link
Author

Ok, that is interesting. The pip installed version gives the same outputs that as you are seeing:

image

The outputs I am originally seeing are from the conda version of the packages.

@Groni3000
Copy link

Ye, really interesting: package manager affects math results ʘ‿ʘ At least you know how to use std with reliable results...

@phofl
Copy link
Member

phofl commented Feb 19, 2024

Are you able to isolate whether it's the pandas or NumPy package that causes this?

@asishm
Copy link
Contributor

asishm commented Feb 20, 2024

The outputs I am originally seeing are from the conda version of the packages.

Can you provide your conda env export file? (or steps to create the env using conda)

@jamespreed
Copy link
Author

jamespreed commented Feb 20, 2024

I am extremely confused now. The issue does NOT appear when clean installing via the following methods:

venv

mkdir test
python -m venv test
cd test
./Scripts/activate
pip install pandas

conda env with numpy, pip install pandas

conda create -n test2 python=3.11 numpy
conda activate test2
pip install conda-forge::pandas==2.2.0 -y

clean conda env

conda create -n test3 python=3.11 conda-forge::pandas==2.2.0 -y

@jamespreed
Copy link
Author

The issue is bottleneck. Whenever I install it into an environment, the float32 issue appears.

In all of the above cases, installing bottleneck using either pip or conda exhibits the problem.

conda activate test2
pip install bottleneck
# issue appears again.

@phofl
Copy link
Member

phofl commented Feb 20, 2024

is this only on the new pandas version or does this also happen if you use an older version of pandas?

@jamespreed
Copy link
Author

It happens on Pandas 1.4.4, 1.5.3, 2.0.0, 2.1.0, 2.1.4, and 2.2.0

@phofl
Copy link
Member

phofl commented Feb 20, 2024

that means that it is most likely a bottleneck issue, could you report there?

@rhshadrach rhshadrach added Reduction Operations sum, mean, min, max, etc. Upstream issue Issue related to pandas dependency and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 25, 2024
@rhshadrach
Copy link
Member

Looking at the linked issue, it appears very likely this is a bottleneck issue. Closing for now.

@rhshadrach
Copy link
Member

Also, note you can disable the use of bottleneck with

pd.set_option("compute.use_bottleneck", False)

You can also temporarily disable it for certain lines:

with pd.option_context("compute.use_bottleneck", False):
    print(pd.Series([271.46] * 150000, dtype='float32').std())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reduction Operations sum, mean, min, max, etc. Upstream issue Issue related to pandas dependency
Projects
None yet
Development

No branches or pull requests

5 participants