SparseDataFrame with dense Series or unknown type · Issue #19374 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

import pandas

pandas.SparseDataFrame(pandas.Series())

Problem description

I've seen there are plans to SparseDataFrame. But reporting anyway.

The above code, creating a SparseDataFrame with a dense Series fails with a not very descriptive error:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-6-b9708ff05a7c> in <module>()
      1 import pandas
      2 
----> 3 pandas.SparseDataFrame(pandas.Series())

~/anaconda3/lib/python3.6/site-packages/pandas/core/sparse/frame.py in __init__(self, data, index, columns, default_kind, default_fill_value, dtype, copy)
    120                 mgr = mgr.astype(dtype)
    121 
--> 122         generic.NDFrame.__init__(self, mgr)
    123 
    124     @property

UnboundLocalError: local variable 'mgr' referenced before assignment

Any other type provided to the constructor fails with the same error.

Expected Output

I'd expect an empty series (if the dense Series has a name, or columns is provided).

Or a useful error message, if the data type is not accepted.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.13-300.fc27.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.utf8
LOCALE: en_GB.UTF-8

pandas: 0.23.0.dev0+152.g2952fbd55.dirty
pytest: 3.3.0
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.7.1
xarray: None
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.1
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None