Skip to content

DOC: update the pandas.DataFrame.to_sparse docstring #20193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,16 +1599,57 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,

def to_sparse(self, fill_value=None, kind='block'):
"""
Convert to SparseDataFrame
Convert to SparseDataFrame.

Implement the sparse version of the DataFrame meaning that any data
matching a specific value it's omitted in the representation.
The sparse DataFrame allows for a more efficient storage.

Parameters
----------
fill_value : float, default NaN
kind : {'block', 'integer'}
fill_value : float, default None
The specific value that should be omitted in the representation.
kind : {'block', 'integer'}, default 'block'
The kind of the SparseIndex tracking where data is not equal to
the fill value:

- 'block' tracks only the locations and sizes of blocks of data.
- 'integer' keeps an array with all the locations of the data.

In most cases 'block' is recommended, since it's more memory
efficient.

Returns
-------
y : SparseDataFrame
SparseDataFrame
The sparse representation of the DataFrame.

See Also
--------
DataFrame.to_dense :
Converts the DataFrame back to the its dense form.

Examples
--------
>>> df = pd.DataFrame([(np.nan, np.nan),
... (1., np.nan),
... (np.nan, 1.)])
>>> df
0 1
0 NaN NaN
1 1.0 NaN
2 NaN 1.0
>>> type(df)
<class 'pandas.core.frame.DataFrame'>

>>> sdf = df.to_sparse()
>>> sdf
0 1
0 NaN NaN
1 1.0 NaN
2 NaN 1.0
>>> type(sdf)
<class 'pandas.core.sparse.frame.SparseDataFrame'>
"""
from pandas.core.sparse.frame import SparseDataFrame
return SparseDataFrame(self._series, index=self.index,
Expand Down