From 404eae008d456b384881bd6df721398e35e05f42 Mon Sep 17 00:00:00 2001 From: jreback Date: Tue, 17 Dec 2013 15:35:19 -0500 Subject: [PATCH] BUG: don't use partial setting with scalars (GH5720) --- doc/source/release.rst | 2 +- pandas/core/frame.py | 19 ++++++++++--------- pandas/tests/test_indexing.py | 8 ++++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 5ce9ccd25a7fc..79079cc52a148 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -246,7 +246,7 @@ API Changes (:issue:`4390`) - allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when the single-key is not currently contained in the index for that axis - (:issue:`2578`, :issue:`5226`, :issue:`5632`) + (:issue:`2578`, :issue:`5226`, :issue:`5632`, :issue:`5720`) - Default export for ``to_clipboard`` is now csv with a sep of `\t` for compat (:issue:`3368`) - ``at`` now will enlarge the object inplace (and return the same) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 90641a833f2a7..2f299488bd321 100644 --- 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): """ diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index e396bee3f4ad9..4954decd5195b 100644 --- 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