Skip to content

Commit 087f441

Browse files
author
Gioia Ballin
committed
Updates the documentation for pandas.DataFrame.to_sparse.
1 parent 3885ced commit 087f441

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
@@ -1570,16 +1570,44 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
15701570

15711571
def to_sparse(self, fill_value=None, kind='block'):
15721572
"""
1573-
Convert to SparseDataFrame
1573+
Convert to SparseDataFrame.
1574+
1575+
Implement the sparse version of the DataFrame meaning that any data
1576+
matching a specific value it's omitted in the representation.
1577+
The sparse DataFrame allows for a more efficient storage.
15741578
15751579
Parameters
15761580
----------
15771581
fill_value : float, default NaN
1582+
The specific value that should be omitted in the representation.
15781583
kind : {'block', 'integer'}
1584+
The kind of the SparseIndex tracking where data is not equal to
1585+
the fill value:
1586+
1587+
- 'block' tracks only the locations and sizes of blocks of data;
1588+
- 'integer' keeps an array with all the locations of the data.
1589+
1590+
The kind 'block' is recommended since it's more memory efficient.
15791591
15801592
Returns
15811593
-------
15821594
y : SparseDataFrame
1595+
1596+
See Also
1597+
--------
1598+
DataFrame.to_dense :
1599+
converts the DataFrame back to the its dense form
1600+
1601+
Examples
1602+
--------
1603+
1604+
Compressing on the zero value.
1605+
1606+
>>> df = pd.DataFrame(np.random.randn(1000, 4))
1607+
>>> df.iloc[:995] = 0.
1608+
>>> sdf = df.to_sparse(fill_value=0.)
1609+
>>> sdf.density
1610+
0.005
15831611
"""
15841612
from pandas.core.sparse.frame import SparseDataFrame
15851613
return SparseDataFrame(self._series, index=self.index,

0 commit comments

Comments
 (0)