-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: mean()
would convert the type of column from decimal to float
#52011
Comments
take |
take |
take |
This is a corrected version of that code. We can create a custom function that converts the output of numpy.mean() to decimal. Code:
Explanation:This function mean_decimal() takes an array as input and returns the mean value as a decimal.Decimal type. We convert the mean value to a string using str() and then pass it to the decimal.Decimal() constructor to ensure it's of the correct type. We can then use this function to calculate the mean of the 'decimal' column in our DataFrame and ensure it's of type decimal.Decimal as follows: Function:
|
We currently do the computation (via NumPy object dtype) as Decimal and then convert the result to float. So if we choose to return Decimal here, I think we can do so without a performance regression. |
I'd suggest using |
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
When the mean function calculates decimal types, it produces a floating-point result, whereas the expected result should be of decimal type.
Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.9.13.final.0
python-bits : 64
OS : Darwin
OS-release : 22.3.0
Version : Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8
pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.9.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : 2.0.0
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: