Repository: pandas-dev/pandas

Issue:
Empty dataframe corrupted by column add (0.13rc1)
Noticed the following difference in behavior between 0.12 and 0.13rc1 when adding a column to an empty dataframe.  Obviously, a weird case, and can be worked around easily.

```
df = pd.DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
y = df[df.A > 5]
y['New'] = np.nan
print y
print y.values
```

&nbsp;

```
(pandas-0.12)$ python test.py 
Empty DataFrame
Columns: [A, B, New]
Index: []
[]
```

&nbsp;

```
(pandas-master)$ python test.py 
     A    B  New
0  NaN  NaN  NaN

[1 rows x 3 columns]
Traceback (most recent call last):
  File "do_fail.py", line 8, in <module>
    print y.values
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/generic.py", line 1705, in values
    return self.as_matrix()
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/generic.py", line 1697, in as_matrix
    self._consolidate_inplace()
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/generic.py", line 1622, in _consolidate_inplace
    self._data = self._protect_consolidate(f)
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/generic.py", line 1660, in _protect_consolidate
    result = f()
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/generic.py", line 1621, in <lambda>
    f = lambda: self._data.consolidate()
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/internals.py", line 2727, in consolidate
    bm = self.__class__(self.blocks, self.axes)
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/internals.py", line 1945, in __init__
    self._verify_integrity()
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/internals.py", line 2227, in _verify_integrity
    tot_items, block.values.shape[1:], self.axes)
  File "/home/gmd/ENV/pandas-master-2/lib/python2.7/site-packages/pandas-0.13.0rc1_82_g66934c2-py2.7-linux-i686.egg/pandas/core/internals.py", line 3561, in construction_error
    tuple(map(int, [len(ax) for ax in axes]))))
ValueError: Shape of passed values is (3, 0), indices imply (3, 1)
```



Discussion:
this was fixed by #5633 (after 0.13rc1)

pls confirm with current master

thanks

confirmed this also fails with current master.


Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -1904,16 +1904,17 @@ def _ensure_valid_index(self, value):
         if not len(self.index):
 
             # GH5632, make sure that we are a Series convertible
-            try:
-                value = Series(value)
-            except:
-                pass
+            if is_list_like(value):
+                try:
+                    value = Series(value)
+                except:
+                    pass
 
-            if not isinstance(value, Series):
-                raise ValueError('Cannot set a frame with no defined index '
-                                 'and a value that cannot be converted to a '
-                                 'Series')
-            self._data.set_axis(1, value.index.copy(), check_axis=False)
+                if not isinstance(value, Series):
+                    raise ValueError('Cannot set a frame with no defined index '
+                                     'and a value that cannot be converted to a '
+                                     'Series')
+                self._data.set_axis(1, value.index.copy(), check_axis=False)
 
     def _set_item(self, key, value):
         """


Test patch:
diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py
--- a/pandas/tests/test_indexing.py
+++ b/pandas/tests/test_indexing.py
@@ -1730,6 +1730,14 @@ def f():
         str(df)
         assert_frame_equal(df,expected)
 
+        # GH5720
+        # don't create rows when empty
+        df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
+        y = df[df.A > 5]
+        y['New'] = np.nan
+        expected = DataFrame(columns=['A','B','New'])
+        assert_frame_equal(y, expected)
+
     def test_cache_updating(self):
         # GH 4939, make sure to update the cache on setitem