Repository: pandas-dev/pandas

Issue:
BUG: SparseDataFrame constructor issues
I've noticed a bunch of issues with SparseDataFrame's constructor, particularly around support for non-`float64` datatypes:

- [ ]  when `data` is not provided, the default_fill_value is not used to fill the data (noted in passing #5470):
```python
>>> pd.SparseDataFrame(columns=list("ab"), index=range(4), default_fill_value=0.0)
    a   b
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
```
~~- [ ] when `data` is not provided and the `dtype` is set to `int64`, the constructor call fails~~
```python
>>> pd.SparseDataFrame(columns=list("ab"), index=range(4), dtype=np.int64)
ValueError: cannot convert float NaN to integer
```

- [ ] ~~when `data` is an empty sparse matrix with `int64` entries and no `dtype` is specified, the type of the SparseDataFrame is still `float64`. Worse, even when `dtype` is specified, it still doesn't work.~~
```python
>>> pd.SparseDataFrame(coo_matrix((4,3), dtype=np.int64))
    0   1   2
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
>>> pd.SparseDataFrame(coo_matrix((4,3), dtype=np.int64), dtype=np.int64)
    0   1   2
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
```
- [ ] ~~probably due to the same issue, given a sparse matrix with underlying elements of type `int64`, if any of the values in a column is `0` then that column will be treated as `float64`s:~~
```python
>>> pd.SparseDataFrame(coo_matrix(np.arange(12).reshape(4,3), dtype=np.int64))
     0   1   2
0  NaN   1   2
1  3.0   4   5
2  6.0   7   8
3  9.0  10  11
```
(as long as at least one value is non-zero, then this can be worked around by setting `default_fill_value`; however, if all of the values in one column are zero then this no longer works).

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.20.2
pytest: 2.9.2
pip: 8.1.2
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.0
scipy: 0.18.1
xarray: None
IPython: 5.1.0
sphinx: 1.4.6
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.1
feather: None
matplotlib: 2.0.2
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.3
lxml: 3.6.4
bs4: 4.5.1
html5lib: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: 0.4.0

</details>



Discussion:
cc @kernc 

> when data is not provided and the dtype is set to int64, the constructor call fails

is as expected. int-NA is not supported.

I marked the above with check-boxes. welcome to have PR for any/all.

IIUC, 2–4 above are due to NaN being non-representable in dtype=int. Can't force it; doesn't work.

So looks like only no. 1 is issue?
yes,

though like to have some tests that assert the fail for 2-4 as well.

Resolving patch:
diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py
--- a/pandas/core/sparse/frame.py
+++ b/pandas/core/sparse/frame.py
@@ -124,8 +124,8 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None,
                 columns = Index([])
             else:
                 for c in columns:
-                    data[c] = SparseArray(np.nan, index=index,
-                                          kind=self._default_kind,
+                    data[c] = SparseArray(self._default_fill_value,
+                                          index=index, kind=self._default_kind,
                                           fill_value=self._default_fill_value)
             mgr = to_manager(data, columns, index)
             if dtype is not None:


Test patch:
diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py
--- a/pandas/tests/sparse/frame/test_frame.py
+++ b/pandas/tests/sparse/frame/test_frame.py
@@ -269,6 +269,19 @@ def test_type_coercion_at_construction(self):
             default_fill_value=0)
         tm.assert_sp_frame_equal(result, expected)
 
+    def test_default_dtype(self):
+        result = pd.SparseDataFrame(columns=list('ab'), index=range(2))
+        expected = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
+                                      columns=list('ab'), index=range(2))
+        tm.assert_sp_frame_equal(result, expected)
+
+    def test_nan_data_with_int_dtype_raises_error(self):
+        sdf = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
+                                 columns=list('ab'), index=range(2))
+        msg = "Cannot convert non-finite values"
+        with pytest.raises(ValueError, match=msg):
+            pd.SparseDataFrame(sdf, dtype=np.int64)
+
     def test_dtypes(self):
         df = DataFrame(np.random.randn(10000, 4))
         df.loc[:9998] = np.nan
@@ -1246,6 +1259,14 @@ def test_notna(self):
                             'B': [True, False, True, True, False]})
         tm.assert_frame_equal(res.to_dense(), exp)
 
+    def test_default_fill_value_with_no_data(self):
+        # GH 16807
+        expected = pd.SparseDataFrame([[1.0, 1.0], [1.0, 1.0]],
+                                      columns=list('ab'), index=range(2))
+        result = pd.SparseDataFrame(columns=list('ab'), index=range(2),
+                                    default_fill_value=1.0)
+        tm.assert_frame_equal(expected, result)
+
 
 class TestSparseDataFrameArithmetic(object):