Skip to content

Commit 4f1e43d

Browse files
committed
BUG: Adding empty dataframes should result in empty blocks pandas-dev#10181
1 parent efc4a08 commit 4f1e43d

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

doc/source/whatsnew/v0.16.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Other API Changes
4141

4242
- ``Holiday`` now raises ``NotImplementedError`` if both ``offset`` and ``observance`` are used in constructor. (:issue:`102171`)
4343

44+
- Adding empty ``DataFrame``s results in a ``DataFrame`` that ``.equals`` an empty ``DataFrame`` (:issue:`10181`)
45+
4446
.. _whatsnew_0162.performance:
4547

4648
Performance Improvements

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

+11
Original file line numberDiff line numberDiff line change
@@ -5142,6 +5142,17 @@ def test_operators(self):
51425142
df = DataFrame({'a': ['a', None, 'b']})
51435143
assert_frame_equal(df + df, DataFrame({'a': ['aa', np.nan, 'bb']}))
51445144

5145+
# Test for issue #10181
5146+
for dtype in ('float', 'int64'):
5147+
frames = [
5148+
DataFrame(dtype=dtype),
5149+
DataFrame(columns=['A'], dtype=dtype),
5150+
DataFrame(index=[0], dtype=dtype),
5151+
]
5152+
for df in frames:
5153+
self.assertTrue((df + df).equals(df))
5154+
assert_frame_equal(df + df, df)
5155+
51455156
def test_ops_np_scalar(self):
51465157
vals, xs = np.random.rand(5, 3), [nan, 7, -23, 2.718, -3.14, np.inf]
51475158
f = lambda x: DataFrame(x, index=list('ABCDE'),

0 commit comments

Comments
 (0)