Skip to content

Commit 8a91b22

Browse files
committed
Merge pull request #5723 from jreback/empty_frame
BUG: don't use partial setting with scalars (GH5720)
2 parents 39a12ef + 404eae0 commit 8a91b22

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ API Changes
246246
(:issue:`4390`)
247247
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when
248248
the single-key is not currently contained in the index for that axis
249-
(:issue:`2578`, :issue:`5226`, :issue:`5632`)
249+
(:issue:`2578`, :issue:`5226`, :issue:`5632`, :issue:`5720`)
250250
- Default export for ``to_clipboard`` is now csv with a sep of `\t` for
251251
compat (:issue:`3368`)
252252
- ``at`` now will enlarge the object inplace (and return the same)

pandas/core/frame.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1904,16 +1904,17 @@ def _ensure_valid_index(self, value):
19041904
if not len(self.index):
19051905

19061906
# GH5632, make sure that we are a Series convertible
1907-
try:
1908-
value = Series(value)
1909-
except:
1910-
pass
1907+
if is_list_like(value):
1908+
try:
1909+
value = Series(value)
1910+
except:
1911+
pass
19111912

1912-
if not isinstance(value, Series):
1913-
raise ValueError('Cannot set a frame with no defined index '
1914-
'and a value that cannot be converted to a '
1915-
'Series')
1916-
self._data.set_axis(1, value.index.copy(), check_axis=False)
1913+
if not isinstance(value, Series):
1914+
raise ValueError('Cannot set a frame with no defined index '
1915+
'and a value that cannot be converted to a '
1916+
'Series')
1917+
self._data.set_axis(1, value.index.copy(), check_axis=False)
19171918

19181919
def _set_item(self, key, value):
19191920
"""

pandas/tests/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,14 @@ def f():
17301730
str(df)
17311731
assert_frame_equal(df,expected)
17321732

1733+
# GH5720
1734+
# don't create rows when empty
1735+
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
1736+
y = df[df.A > 5]
1737+
y['New'] = np.nan
1738+
expected = DataFrame(columns=['A','B','New'])
1739+
assert_frame_equal(y, expected)
1740+
17331741
def test_cache_updating(self):
17341742
# GH 4939, make sure to update the cache on setitem
17351743

0 commit comments

Comments
 (0)