Skip to content

Commit cada053

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

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

pandas/core/frame.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1418,16 +1418,44 @@ 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+
- 'integer' keeps an array with all the locations of the data.
1437+
1438+
The kind 'block' is recommended since it's more memory efficient.
14271439
14281440
Returns
14291441
-------
14301442
y : SparseDataFrame
1443+
1444+
See Also
1445+
--------
1446+
DataFrame.to_dense :
1447+
converts the DataFrame back to the its dense form
1448+
1449+
Examples
1450+
--------
1451+
1452+
Compressing on the zero value.
1453+
1454+
>>> df = pd.DataFrame(np.random.randn(1000, 4))
1455+
>>> df.iloc[:995] = 0.
1456+
>>> sdf = df.to_sparse(fill_value=0.)
1457+
>>> sdf.density
1458+
0.005
14311459
"""
14321460
from pandas.core.sparse.frame import SparseDataFrame
14331461
return SparseDataFrame(self._series, index=self.index,

0 commit comments

Comments
 (0)