Skip to content

Commit f6e84c5

Browse files
committed
Merge pull request #3184 from waitingkuo/fix-append-empty-frame
BUG: Append the empty frame with columns, #3121
2 parents 40a07ba + 95c23a5 commit f6e84c5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pandas/core/common.py

+3
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,9 @@ def _concat_compat(to_concat, axis=0):
17001700
# filter empty arrays
17011701
to_concat = [x for x in to_concat if x.shape[axis] > 0]
17021702

1703+
# return the empty np array, if nothing to concatenate, #3121
1704+
if not to_concat: return np.array([], dtype=object)
1705+
17031706
is_datetime64 = [x.dtype == _NS_DTYPE for x in to_concat]
17041707
if all(is_datetime64):
17051708
# work around NumPy 1.6 bug

pandas/tests/test_frame.py

+30
Original file line numberDiff line numberDiff line change
@@ -5166,6 +5166,36 @@ def test_append_list_of_series_dicts(self):
51665166
expected = df.append(DataFrame(dicts), ignore_index=True)
51675167
assert_frame_equal(result, expected)
51685168

5169+
def test_append_empty_dataframe(self):
5170+
5171+
# Empty df append empty df
5172+
df1 = DataFrame([])
5173+
df2 = DataFrame([])
5174+
result = df1.append(df2)
5175+
expected = df1.copy()
5176+
assert_frame_equal(result, expected)
5177+
5178+
# Non-empty df append empty df
5179+
df1 = DataFrame(np.random.randn(5, 2))
5180+
df2 = DataFrame()
5181+
result = df1.append(df2)
5182+
expected = df1.copy()
5183+
assert_frame_equal(result, expected)
5184+
5185+
# Empty df with columns append empty df
5186+
df1 = DataFrame(columns=['bar', 'foo'])
5187+
df2 = DataFrame()
5188+
result = df1.append(df2)
5189+
expected = df1.copy()
5190+
assert_frame_equal(result, expected)
5191+
5192+
# Non-Empty df with columns append empty df
5193+
df1 = DataFrame(np.random.randn(5, 2), columns=['bar', 'foo'])
5194+
df2 = DataFrame()
5195+
result = df1.append(df2)
5196+
expected = df1.copy()
5197+
assert_frame_equal(result, expected)
5198+
51695199
def test_asfreq(self):
51705200
offset_monthly = self.tsframe.asfreq(datetools.bmonthEnd)
51715201
rule_monthly = self.tsframe.asfreq('BM')

0 commit comments

Comments
 (0)