Skip to content

BUG: Inconsistent data type conversion with .sum #37491

Closed
@candalfigomoro

Description

@candalfigomoro
  • 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.


Code Sample, a copy-pastable example

import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
print(s.sum().dtype)                    # int64
print(s.astype("float16").sum().dtype)  # float16
print(s.astype("float32").sum().dtype)  # float32
print(s.astype("int16").sum().dtype)    # int64 ???
print(s.astype("uint8").sum().dtype)    # int64 ???

Problem description

Summing a float16 Series/DataFrame returns float16 values.
Summing a float32 Series/DataFrame returns float32 values.
However, summing integer Series/DataFrames always returns int64 values, regardless of their original type ( int16, uint8 etc.).

Expected Output

Summing int32 values should return int32 values, just like summing float32 values returns float32 values.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.16299
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Italian_Italy.1252

pandas : 1.1.3
numpy : 1.19.3
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.0.post20201006
Cython : None
pytest : None
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
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions