Skip to content

Commit eb8ad50

Browse files
committed
BUG: Adding empty dataframes should result in empty blocks pandas-dev#10181
1 parent 0aceb38 commit eb8ad50

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pandas/core/internals.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -3530,11 +3530,15 @@ def construction_error(tot_items, block_shape, axes, e=None):
35303530
def create_block_manager_from_blocks(blocks, axes):
35313531
try:
35323532
if len(blocks) == 1 and not isinstance(blocks[0], Block):
3533-
# It's OK if a single block is passed as values, its placement is
3534-
# basically "all items", but if there're many, don't bother
3535-
# converting, it's an error anyway.
3536-
blocks = [make_block(values=blocks[0],
3537-
placement=slice(0, len(axes[0])))]
3533+
# if blocks[0] is of length 0, return empty blocks
3534+
if not len(blocks[0]):
3535+
blocks = []
3536+
else:
3537+
# It's OK if a single block is passed as values, its placement is
3538+
# basically "all items", but if there're many, don't bother
3539+
# converting, it's an error anyway.
3540+
blocks = [make_block(values=blocks[0],
3541+
placement=slice(0, len(axes[0])))]
35383542

35393543
mgr = BlockManager(blocks, axes)
35403544
mgr._consolidate_inplace()

pandas/tests/test_frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,7 @@ def custom_frame_function(self):
28212821
data = {'col1': range(10),
28222822
'col2': range(10)}
28232823
cdf = CustomDataFrame(data)
2824-
2824+
28252825
# Did we get back our own DF class?
28262826
self.assertTrue(isinstance(cdf, CustomDataFrame))
28272827

@@ -2833,7 +2833,7 @@ def custom_frame_function(self):
28332833
# Do we get back our own DF class after slicing row-wise?
28342834
cdf_rows = cdf[1:5]
28352835
self.assertTrue(isinstance(cdf_rows, CustomDataFrame))
2836-
self.assertEqual(cdf_rows.custom_frame_function(), 'OK')
2836+
self.assertEqual(cdf_rows.custom_frame_function(), 'OK')
28372837

28382838
# Make sure sliced part of multi-index frame is custom class
28392839
mcol = pd.MultiIndex.from_tuples([('A', 'A'), ('A', 'B')])
@@ -5129,6 +5129,11 @@ def test_operators(self):
51295129
df = DataFrame({'a': ['a', None, 'b']})
51305130
assert_frame_equal(df + df, DataFrame({'a': ['aa', np.nan, 'bb']}))
51315131

5132+
df = DataFrame()
5133+
self.assertTrue((df + df).equals(DataFrame()))
5134+
assert_frame_equal(df + df, DataFrame())
5135+
5136+
51325137
def test_ops_np_scalar(self):
51335138
vals, xs = np.random.rand(5, 3), [nan, 7, -23, 2.718, -3.14, np.inf]
51345139
f = lambda x: DataFrame(x, index=list('ABCDE'),

0 commit comments

Comments
 (0)