Skip to content

Commit a60887d

Browse files
committed
Merge pull request #5935 from jreback/empty_frame_copy
BUG: Bug in creating an empty DataFrame, copying, then assigning (GH5932)
2 parents 451dce4 + 8eb2b46 commit a60887d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Bug Fixes
8989
- Bug in idxmin/max with object dtypes (:issue:`5914`)
9090
- Bug in ``BusinessDay`` when adding n days to a date not on offset when n>5 and n%5==0 (:issue:`5890`)
9191
- Bug in assigning to chained series with a series via ix (:issue:`5928`)
92+
- Bug in creating an empty DataFrame, copying, then assigning (:issue:`5932`)
9293

9394
pandas 0.13.0
9495
-------------

pandas/core/internals.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,8 +1961,11 @@ def make_empty(self, axes=None):
19611961
]
19621962

19631963
# preserve dtype if possible
1964-
dtype = self.dtype if self.ndim == 1 else object
1965-
return self.__class__(np.array([], dtype=dtype), axes)
1964+
if self.ndim == 1:
1965+
blocks = np.array([], dtype=self.dtype)
1966+
else:
1967+
blocks = []
1968+
return self.__class__(blocks, axes)
19661969

19671970
def __nonzero__(self):
19681971
return True

pandas/tests/test_indexing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,14 @@ def f():
18431843
df = DataFrame(Series(name='foo'))
18441844
assert_frame_equal(df, DataFrame({ 'foo' : Series() }))
18451845

1846+
# GH 5932
1847+
# copy on empty with assignment fails
1848+
df = DataFrame(index=[0])
1849+
df = df.copy()
1850+
df['a'] = 0
1851+
expected = DataFrame(0,index=[0],columns=['a'])
1852+
assert_frame_equal(df, expected)
1853+
18461854
def test_cache_updating(self):
18471855
# GH 4939, make sure to update the cache on setitem
18481856

0 commit comments

Comments
 (0)