Skip to content

BUG: DataFrame.mode() fails on sparse integer DataFrames #55181

Closed
@Lucas-Gui

Description

@Lucas-Gui

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
from scipy.sparse import coo_array
m = coo_array(([1,1,1,2], ([0,3,4,5],[0,0,0,1])))
df = pd.DataFrame.sparse.from_spmatrix(m, columns=list('ab'))
print(df.mode())
print(df.sparse.to_dense().mode())

Issue Description

df.mode() yields nans for columns with a unique mode when used on a DataFrame with Sparse[int64, 0] column dtype and some columns with several modes.
In the previous case, we obtain :

a b
0 NaN
1 NaN

Expected Behavior

The result of df.mode() should be the same as df.sparse.to_dense().mode(), i.e., in the above example :

a b
0 0.0
1 NaN

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.11.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-35-generic
Version : #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.0
numpy : 1.24.4
pytz : 2022.7
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.2
pandas_datareader : None
bs4 : 4.12.2
bottleneck : 1.3.5
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : 0.57.1
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : 2.2.0
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugNeeds TriageIssue that has not been reviewed by a pandas team memberSparseSparse Data Type

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions