Skip to content

DataFrame.to_sparse fill_value keyword doesn't seem to fill NA #20172

Closed
@minggli

Description

@minggli
>>> from pandas import *
>>> from numpy.random import randn
>>>
>>> df = DataFrame(randn(20, 4))
>>> df = df.applymap(lambda x: np.nan if x > 0 else x)
>>> sparse_df = df.to_sparse(fill_value=0.35)
>>> sparse_df
           0         1         2         3
0  -1.101095       NaN -1.512058 -0.283055
1  -0.460331       NaN -0.995369       NaN
2        NaN -0.835043 -1.367358 -0.950400
3  -0.704320 -0.746556 -0.389821       NaN
4        NaN -0.573624       NaN -1.033302
5        NaN       NaN -1.684434 -1.805903
6        NaN       NaN       NaN       NaN
7  -0.129019 -2.279948 -0.229312 -0.725433
8  -1.264768 -0.050307       NaN -0.453808
9  -0.976076 -0.127872 -0.794526       NaN
10 -0.126079 -0.774120 -0.424461 -0.061588
11 -0.474522       NaN -0.437307 -0.727405
12       NaN -0.883299       NaN -1.435336
13 -1.588188       NaN -1.740614 -0.645563
14 -1.218019       NaN       NaN -0.341365
15 -0.522648 -1.377164       NaN       NaN
16       NaN -1.193746 -1.295383       NaN
17 -1.445435 -0.034316 -0.879550 -1.570954
18       NaN -0.060612       NaN       NaN
19       NaN       NaN -0.009722       NaN

Problem description

I've looked at issues log and don't seem to find an issue regarding this. It appears that DataFrame.to_sparse keyword fill_value which subsequently injected into SparseDataFrame doesn't seem to do anything.

Is it actually a bug or the documentation isn't accurate enough?

Expected Output


           0         1         2         3
0  -1.101095  0.350000 -1.512058 -0.283055
1  -0.460331  0.350000 -0.995369  0.350000
2   0.350000 -0.835043 -1.367358 -0.950400
3  -0.704320 -0.746556 -0.389821  0.350000
4   0.350000 -0.573624  0.350000 -1.033302
5   0.350000  0.350000 -1.684434 -1.805903
6   0.350000  0.350000  0.350000  0.350000
7  -0.129019 -2.279948 -0.229312 -0.725433
8  -1.264768 -0.050307  0.350000 -0.453808
9  -0.976076 -0.127872 -0.794526  0.350000
10 -0.126079 -0.774120 -0.424461 -0.061588
11 -0.474522  0.350000 -0.437307 -0.727405
12  0.350000 -0.883299  0.350000 -1.435336
13 -1.588188  0.350000 -1.740614 -0.645563
14 -1.218019  0.350000  0.350000 -0.341365
15 -0.522648 -1.377164  0.350000  0.350000
16  0.350000 -1.193746 -1.295383  0.350000
17 -1.445435 -0.034316 -0.879550 -1.570954
18  0.350000 -0.060612  0.350000  0.350000
19  0.350000  0.350000 -0.009722  0.350000

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 36.3.0
Cython: None
numpy: 1.14.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 5.3.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    SparseSparse Data Type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions