Skip to content

Add column to empty DataFrame? #2147

Closed
Closed
@klonuo

Description

@klonuo

I'm reading 0.9 PDF documentation, about DataFrame object.

I make empty DataFrame:

>>> from pandas import DataFrame
>>> df = DataFrame()
>>> df
Empty DataFrame
Columns: array([], dtype=object)
Index: array([], dtype=object)
>>> df['A'] = [1, 2, 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1804, in __setitem__
    self._set_item(key, value)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1843, in _set_item
    value = self._sanitize_column(key, value)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1872, in _sanitize_column
    assert(len(value) == len(self.index))
AssertionError

Error w/o any description.

If df has already columns, then I can add another:

>>> from pandas import DataFrame
>>> df = DataFrame.from_items([('A', [1, 2, 3])])
>>> df['B'] = [3, 2, 1]
>>> df
   A  B
0  1  3
1  2  2
2  3  1

I'm not sure if this is some kind of feature or it is a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions