Skip to content

Can't create a DataFrame from an empty Series #2234

@grsr

Description

@grsr

For a project I am working on it would be convenient if you could create a DataFrame from an empty Series. I have some library code that will create a Series object constructed from a dict, most of the time this dict will have some entries, but occasionally it does not. Currently you can create an empty DataFrame by not supplying anything to the constructor, and you can create a DataFrame or a Series from an empty dict, but if you try to create a DataFrame from an Series constructed from an empty dict pandas throws an AssertionError. I can code around this easily enough, but it would be preferable just to return an empty DataFrame. Example code below:

In [73]: pandas.__version__
Out[73]: '0.9.0'

In [74]: df = DataFrame()

In [75]: df = DataFrame({})

In [76]: s = Series({}, name="foo")

In [77]: df = DataFrame(s)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-77-4c4337b321e1> in <module>()
----> 1 df = DataFrame(s)

/nfs/users/nfs_g/gr5/software/epd-7.2-2-rh5-x86_64/lib/python2.7/site-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
    392             else:
    393                 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 394                                          copy=copy)
    395         elif isinstance(data, list):
    396             if len(data) > 0:

/nfs/users/nfs_g/gr5/software/epd-7.2-2-rh5-x86_64/lib/python2.7/site-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in _init_ndarray(self, values, index, columns, dtype, copy)
    504             columns = _ensure_index(columns)
    505 
--> 506         block = make_block(values.T, columns, columns)
    507         return BlockManager([block], [columns, index])
    508 

/nfs/users/nfs_g/gr5/software/epd-7.2-2-rh5-x86_64/lib/python2.7/site-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/internals.pyc in make_block(values, items, ref_items, do_integrity_check)
    459 
    460     return klass(values, items, ref_items, ndim=values.ndim,
--> 461                  do_integrity_check=do_integrity_check)
    462 
    463 # TODO: flexible with index=None and/or items=None

/nfs/users/nfs_g/gr5/software/epd-7.2-2-rh5-x86_64/lib/python2.7/site-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/internals.pyc in __init__(self, values, items, ref_items, ndim, do_integrity_check)
     24 
     25         assert(values.ndim == ndim)
---> 26         assert(len(items) == len(values))
     27 
     28         self.values = values

AssertionError: 

Cheers,

Graham

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions