Skip to content

Constructing DataFrame from dict #9260

@bmu

Description

@bmu

If think it was possible to construct a DataFrame from a dict, which gives an error with v0.15

In [4]: td = {'one': 1, 'two': 2}

In [5]: pd.DataFrame(td)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-71317f07d925> in <module>()
----> 1 pd.DataFrame(td)

.../python2.7/site-packages/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
    206                                  dtype=dtype, copy=copy)
    207         elif isinstance(data, dict):
--> 208             mgr = self._init_dict(data, index, columns, dtype=dtype)
    209         elif isinstance(data, ma.MaskedArray):
    210             import numpy.ma.mrecords as mrecords

.../lib/python2.7/site-packages/pandas/core/frame.pyc in _init_dict(self, data, index, columns, dtype)
    334 
    335         return _arrays_to_mgr(arrays, data_names, index, columns,
--> 336                               dtype=dtype)
    337 
    338     def _init_ndarray(self, values, index, columns, dtype=None,

.../python2.7/site-packages/pandas/core/frame.pyc in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   4615     # figure out the index, if necessary
   4616     if index is None:
-> 4617         index = extract_index(arrays)
   4618     else:
   4619         index = _ensure_index(index)

.../python2.7/site-packages/pandas/core/frame.pyc in extract_index(data)
   4654 
   4655         if not indexes and not raw_lengths:
-> 4656             raise ValueError('If using all scalar values, you must pass'
   4657                              ' an index')
   4658 

ValueError: If using all scalar values, you must pass an index

The same is true if I use DataFrame.from_dict to construct the DataFrame.

Not sure if this was changed or if it is a bug.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions