Skip to content

New appending behavior doesn't work on an empty DataFrame #5226

Closed
@danielballan

Description

@danielballan

Consider this working example from the docs:

In [9]: df1 = DataFrame(np.arange(6).reshape(3,2),
   ...:                 columns=['A','B'])

In [10]: df1.loc[3] = [6, 7]

In [11]: df1
Out[11]: 
   A  B
0  0  1
1  2  3
2  4  5
3  6  7

And now watch what happens when the DataFrame is empty:

In [12]: df2 = DataFrame(columns=['A','B'])

In [13]: df2.loc[3] = [6, 7]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-53d84383e8d4> in <module>()
----> 1 df2.loc[3] = [6, 7]

/Users/danielallan/Documents/Repos/pandas-danielballan/pandas/core/indexing.pyc in __setitem__(self, key, value)
     90             indexer = self._convert_tuple(key, is_setter=True)
     91         else:
---> 92             indexer = self._convert_to_indexer(key, is_setter=True)
     93 
     94         self._setitem_with_indexer(indexer, value)

/Users/danielallan/Documents/Repos/pandas-danielballan/pandas/core/indexing.pyc in _convert_to_indexer(self, obj, axis, is_setter)
    821             if is_setter:
    822                 if obj >= len(self.obj) and not isinstance(labels, MultiIndex):
--> 823                     raise ValueError("cannot set by positional indexing with enlargement")
    824 
    825             return obj

ValueError: cannot set by positional indexing with enlargement

I haven't read the discussion that generated this new behavior of loc/ix. Perhaps there is a good reason why this can't work. But I can't think of one.

(This was prompted by this SO question.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions