Description
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
print(pd.__version__) # 3.0.0.dev0+1020.g2aa155ae1c
s1 = pd.Series([0, 0, 1, 1, 2, None], dtype='Int16', name='A')
s2 = pd.Series([10, 20, 30, None, None, 60], dtype='Int16', name='B')
df = pd.concat([s1, s2], axis=1)
print(df.iloc[:5].groupby('A')['B'].cumsum().dtype) # Int16 as expected
print(df.iloc[:6].groupby('A')['B'].cumsum().dtype) # Float64, BUT EXPECTED Int16Dtype !!!
print(df.iloc[5:].groupby('A')['B'].cumsum().dtype) # Float64, BUT EXPECTED Int16Dtype !!!
print(df.iloc[:5].groupby('A', dropna=False)['B'].cumsum().dtype) # Int16 as expected
print(df.iloc[:6].groupby('A', dropna=False)['B'].cumsum().dtype) # Int16 as expected
print(df.iloc[5:].groupby('A', dropna=False)['B'].cumsum().dtype) # Int16 as expected
print(df.iloc[:5].groupby('A')['B'].sum().dtype) # Int16 as expected
print(df.iloc[:6].groupby('A')['B'].sum().dtype) # Int16 as expected
print(df.iloc[5:].groupby('A')['B'].sum().dtype) # Int16 as expected
Issue Description
If a None
(pd.NA
) is present in group labels (i.e. in the by=
column), then the result of a transformation (here cumsum
) changes dtype from nullable integer (Int16
) to nullable float (Float64
).
This is unexpected and therefore a bug. The type change can be undone by .astype(...)
, but although unlikely in my use case, in theory a loss of precision is possible. I also wonder if floating point operations and type conversion can have a noticeable impact on performance for large in-memory datasets.
Other remarks:
- No bug when
groupby(..., dropna=False)
, as shown above. - No bug when using aggregation, e.g.
.sum()
instead ofcumsum()
, as shown above.
Expected Behavior
See the example.
Installed Versions
INSTALLED VERSIONS
commit : 2aa155a
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.133+
Version : #1 SMP Tue Dec 19 13:14:11 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : POSIX
LANG : C.UTF-8
LOCALE : None.None
pandas : 3.0.0.dev0+1020.g2aa155ae1c
numpy : 1.26.4
pytz : 2023.3.post1
dateutil : 2.9.0.post0
setuptools : 69.0.3
pip : 23.3.2
Cython : 3.0.8
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 5.2.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.20.0
pandas_datareader : 0.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
fastparquet : None
fsspec : 2024.2.0
gcsfs : 2024.2.0
matplotlib : 3.7.5
numba : 0.58.1
numexpr : 2.10.0
odfpy : None
openpyxl : 3.1.2
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.2.0
scipy : 1.11.4
sqlalchemy : 2.0.25
tables : 3.9.2
tabulate : 0.9.0
xarray : 2024.3.0
xlrd : None
zstandard : 0.22.0
tzdata : 2023.4
qtpy : None
pyqt5 : None