BUG: Nullable integer type ("Int64") lost after summing along columns-index [df.sum(axis=1) #50438
Closed
2 of 3 tasks
Labels
Bug
Duplicate Report
Duplicate issue or pull request
NA - MaskedArrays
Related to pd.NA and nullable extension arrays
Reduction Operations
sum, mean, min, max, etc.
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
With normal integers
dh.sum(axis=1); dh.sum()
the obtained sums are integers as well unless a value is missing, then things go odd (withdg.sum(); dg.sum(axis=1)
) one gets an object or a float.The proposed solution for this, the Nullable integer type ('Int64'), only partly works here.
Summing along the index axis (0, default),
di.sum()
, keeps 'Int64' as would be expected.But this type is not kept when summing over rows, along the columns-axis.
See code example: with
di.sum(axis=1)
the resulting sums are dtype 'float' not 'Int64'.Using the expected behaviour for axis=0, one can keep 'Int64' after summing rows by means of double transposition .
A pd.NA missing value in a dataframe of dtype 'Int64', also yields a float after summing rows (
df.sum(axis=1)
)Expected Behavior
Nullability of 'Int64', would mean that integers are not becoming floats due to other datatypes or after some normal operation on the dataframe (that would not affect integers, like summing).
Installed Versions
INSTALLED VERSIONS
commit : 8dab54d
python : 3.9.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.17
Version : #1 SMP PREEMPT_DYNAMIC Mon Oct 24 13:00:29 CDT 2022
machine : x86_64
processor : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.1.1
pip : 22.2.2
Cython : 0.29.28
pytest : 7.2.0
hypothesis : None
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.7.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : 1.4.45
tables : None
tabulate : None
xarray : 2022.12.0
xlrd : 1.1.0
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: