Skip to content

Commit c753b3f

Browse files
hexgnuharisbal
authored and
harisbal
committed
BUG: Fix problem with SparseDataFrame not persisting to csv (pandas-dev#19441)
* BUG: Fix problem with SparseDataFrame not persisting to csv * FIX: Remove comment and move test with more coverage * FIX: Flake8 issues cleanup * Fix failing test due to blank lines * FIX: linting errors on whitespace * Use parametrize on test * Move bug description to sparse header * Add GH issue to test * Fix linting error
1 parent a11f48d commit c753b3f

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ I/O
529529
- Bug in :func:`DataFrame.to_parquet` where an exception was raised if the write destination is S3 (:issue:`19134`)
530530
- :class:`Interval` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`)
531531
- :class:`Timedelta` now supported in :func:`DataFrame.to_excel` for xls file type (:issue:`19242`, :issue:`9155`)
532-
-
533532

534533
Plotting
535534
^^^^^^^^
@@ -553,7 +552,7 @@ Sparse
553552
^^^^^^
554553

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

559558
Reshaping

pandas/core/internals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ def to_native_types(self, slicer=None, na_rep='nan', quoting=None,
709709
**kwargs):
710710
""" convert to our native types format, slicing if desired """
711711

712-
values = self.values
712+
values = self.get_values()
713+
713714
if slicer is not None:
714715
values = values[:, slicer]
715716
mask = isna(values)
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import pytest
3+
from pandas import SparseDataFrame, read_csv
4+
from pandas.util import testing as tm
5+
6+
7+
class TestSparseDataFrameToCsv(object):
8+
fill_values = [np.nan, 0, None, 1]
9+
10+
@pytest.mark.parametrize('fill_value', fill_values)
11+
def test_to_csv_sparse_dataframe(self, fill_value):
12+
# GH19384
13+
sdf = SparseDataFrame({'a': type(self).fill_values},
14+
default_fill_value=fill_value)
15+
16+
with tm.ensure_clean('sparse_df.csv') as path:
17+
sdf.to_csv(path, index=False)
18+
df = read_csv(path, skip_blank_lines=False)
19+
20+
tm.assert_sp_frame_equal(df.to_sparse(fill_value=fill_value), sdf)

0 commit comments

Comments
 (0)