Skip to content

BUG: SparseDataFrame does not allow single value data #5470

Closed
@BrenBarn

Description

@BrenBarn

You can create a normal DataFrame with, e.g., pandas.DataFrame(0, index=[1, 2, 3], columns=["A", "B", "C"]). However, this fails with a SparseDataFrame:

>>> x = pandas.SparseDataFrame(0, index=[1, 2, 3], columns=["A", "B", "C"])
Traceback (most recent call last):
  File "<pyshell#97>", line 1, in <module>
    x = pandas.SparseDataFrame(0, index=[1, 2, 3], columns=["A", "B", "C"])
  File "c:\users\brenbarn\documents\python\extensions\pandas\pandas\sparse\frame.py", line 123, in __init__
    NDFrame.__init__(self, mgr)
UnboundLocalError: local variable 'mgr' referenced before assignment

It looks like the code in SparseDataFrame.__init__ is not handling the case where the data is a single value. It has a bunch of if statements to handle different kinds of data, but falls through without creating mgr if none of them are met.

This makes it difficult to create an "empty" SparseDataFrame whose default fill value is something other than nan, e.g., pandas.SparseDataFrame(0, index=[1, 2, 3], columns=["A", "B", "C"], default_fill_value=0). In fact, I would suggest that if a default_fill_value is provided but no data is provided, the SparseDataFrame should be filled with the fill value (not nan), but perhaps that warrants a separate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, ExplodeSparseSparse Data Type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions