Skip to content

BUG: Fix problem with SparseDataFrame not persisting to csv #19441

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 10 commits into from
Feb 1, 2018
Merged
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ I/O
- Bug in :func:`DataFrame.to_parquet` where an exception was raised if the write destination is S3 (:issue:`19134`)
- :class:`Interval` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`)
- :class:`Timedelta` now supported in :func:`DataFrame.to_excel` for xls file type (:issue:`19242`, :issue:`9155`)
-

Plotting
^^^^^^^^
Expand All @@ -521,7 +520,7 @@ Sparse
^^^^^^

- Bug in which creating a ``SparseDataFrame`` from a dense ``Series`` or an unsupported type raised an uncontrolled exception (:issue:`19374`)
-
- Bug in :class:`SparseDataFrame.to_csv` causing exception (:issue:`19384`)
-

Reshaping
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ def to_native_types(self, slicer=None, na_rep='nan', quoting=None,
**kwargs):
""" convert to our native types format, slicing if desired """

values = self.values
values = self.get_values()

if slicer is not None:
values = values[:, slicer]
mask = isna(values)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/sparse/frame/test_to_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pytest
from pandas import SparseDataFrame, read_csv
from pandas.util import testing as tm


class TestSparseDataFrameToCsv(object):
fill_values = [np.nan, 0, None, 1]

@pytest.mark.parametrize('fill_value', fill_values)
def test_to_csv_sparse_dataframe(self, fill_value):
# GH19384
sdf = SparseDataFrame({'a': type(self).fill_values},
default_fill_value=fill_value)

with tm.ensure_clean('sparse_df.csv') as path:
sdf.to_csv(path, index=False)
df = read_csv(path, skip_blank_lines=False)

tm.assert_sp_frame_equal(df.to_sparse(fill_value=fill_value), sdf)