Skip to content

Commit 7e5d066

Browse files
committed
BUG: Consistency with dtypes in setting an empty DataFrame (GH6171)
1 parent 0a5e36a commit 7e5d066

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Bug Fixes
174174
being thrown away (:issue:`6148`).
175175
- Bug in ``HDFStore`` on appending a dataframe with multi-indexed columns to
176176
an existing table (:issue:`6167`)
177+
- Consistency with dtypes in setting an empty DataFrame (:issue:`6171`)
177178

178179
pandas 0.13.0
179180
-------------

pandas/core/internals.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,18 @@ def set(self, item, value, check=False):
14211421
return
14221422
except:
14231423
pass
1424-
self.values[loc] = value
1424+
try:
1425+
self.values[loc] = value
1426+
except (ValueError):
1427+
1428+
# broadcasting error
1429+
# see GH6171
1430+
new_shape = list(value.shape)
1431+
new_shape[0] = len(self.items)
1432+
self.values = np.empty(tuple(new_shape),dtype=self.dtype)
1433+
self.values.fill(np.nan)
1434+
self.values[loc] = value
1435+
14251436

14261437
def _maybe_downcast(self, blocks, downcast=None):
14271438

pandas/tests/test_indexing.py

+17
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,23 @@ def f():
19871987
expected = DataFrame(0,index=[0],columns=['a'])
19881988
assert_frame_equal(df, expected)
19891989

1990+
# GH 6171
1991+
# consistency on empty frames
1992+
df = DataFrame(columns=['x', 'y'])
1993+
df['x'] = [1, 2]
1994+
expected = DataFrame(dict(x = [1,2], y = [np.nan,np.nan]))
1995+
assert_frame_equal(df, expected, check_dtype=False)
1996+
1997+
df = DataFrame(columns=['x', 'y'])
1998+
df['x'] = ['1', '2']
1999+
expected = DataFrame(dict(x = ['1','2'], y = [np.nan,np.nan]),dtype=object)
2000+
assert_frame_equal(df, expected)
2001+
2002+
df = DataFrame(columns=['x', 'y'])
2003+
df.loc[0, 'x'] = 1
2004+
expected = DataFrame(dict(x = [1], y = [np.nan]))
2005+
assert_frame_equal(df, expected, check_dtype=False)
2006+
19902007
def test_cache_updating(self):
19912008
# GH 4939, make sure to update the cache on setitem
19922009

0 commit comments

Comments
 (0)