Skip to content

Commit c2cc096

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

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

pandas/core/frame.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -1418,16 +1418,45 @@ 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
1424+
matching a specific value it's omitted in the representation.
1425+
The sparse DataFrame allows for a more efficient storage.
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 is not equal to
1433+
the fill value:
1434+
1435+
- 'block' tracks only the locations and sizes of blocks of data;
1436+
1437+
- 'integer' keeps an array with all the locations of the data.
1438+
1439+
The kind 'block' is recommended since it's more memory efficient.
14271440
14281441
Returns
14291442
-------
14301443
y : SparseDataFrame
1444+
1445+
See Also
1446+
--------
1447+
pandas.SparseDataFrame.to_dense :
1448+
converts the DataFrame back to the its dense form
1449+
1450+
Examples
1451+
--------
1452+
1453+
Compressing on the zero value.
1454+
1455+
>>> df = pd.DataFrame(np.random.randn(1000, 4))
1456+
>>> df.iloc[:995] = 0.
1457+
>>> sdf = df.to_sparse(fill_value=0.)
1458+
>>> sdf.density
1459+
0.005
14311460
"""
14321461
from pandas.core.sparse.frame import SparseDataFrame
14331462
return SparseDataFrame(self._series, index=self.index,

0 commit comments

Comments
 (0)