Skip to content

Commit dbac372

Browse files
author
Gioia Ballin
committed
Updates the documentation for pandas.DataFrame.to_sparse.
1 parent 8497029 commit dbac372

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

pandas/core/frame.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1418,16 +1418,41 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
14181418

14191419
def to_sparse(self, fill_value=None, kind='block'):
14201420
"""
1421-
Convert to SparseDataFrame
1421+
Convert to SparseDataFrame.
1422+
1423+
Implement the sparse version of the DataFrame meaning that any data matching
1424+
a specific value it's omitted in the representation. The sparse DataFrame takes
1425+
less memory on disk when pickled and in the Python interpreter.
14221426
14231427
Parameters
14241428
----------
14251429
fill_value : float, default NaN
1430+
The specific value that should be omitted in the representation.
14261431
kind : {'block', 'integer'}
1432+
The kind of the SparseIndex tracking where data has been omitted.
1433+
The block kind is recommended since it’s more memory efficient:
1434+
it tracks just the locations and sizes of the blocks of data that
1435+
are not equal to the fill value while the integer kind keeps an
1436+
array with all those locations.
14271437
14281438
Returns
14291439
-------
14301440
y : SparseDataFrame
1441+
1442+
See Also
1443+
--------
1444+
pandas.DataFrame.to_dense: converts the DataFrame back to the its dense form
1445+
1446+
Examples
1447+
--------
1448+
1449+
Compressing on the zero value.
1450+
1451+
>>> df = pd.DataFrame(np.random.randn(1000, 4))
1452+
>>> df.iloc[:995] = 0.
1453+
>>> sdf = df.to_sparse(fill_value=0.)
1454+
>>> sdf.density
1455+
0.005
14311456
"""
14321457
from pandas.core.sparse.frame import SparseDataFrame
14331458
return SparseDataFrame(self._series, index=self.index,

0 commit comments

Comments
 (0)