Skip to content

Commit d24dc4b

Browse files
committed
Merge pull request #10188 from rekcahpassyla/empty_df_add
BUG: Adding empty dataframes should result in empty blocks #10181
2 parents 342c91b + d0ba41e commit d24dc4b

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
@@ -98,6 +98,8 @@ Other API Changes
9898

9999
- ``Holiday`` now raises ``NotImplementedError`` if both ``offset`` and ``observance`` are used in constructor instead of returning an incorrect result (:issue:`10217`).
100100

101+
- Adding empty ``DataFrame``s results in a ``DataFrame`` that ``.equals`` an empty ``DataFrame`` (:issue:`10181`)
102+
101103
.. _whatsnew_0162.performance:
102104

103105
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
@@ -5148,6 +5148,17 @@ def test_operators(self):
51485148
df = DataFrame({'a': ['a', None, 'b']})
51495149
assert_frame_equal(df + df, DataFrame({'a': ['aa', np.nan, 'bb']}))
51505150

5151+
# Test for issue #10181
5152+
for dtype in ('float', 'int64'):
5153+
frames = [
5154+
DataFrame(dtype=dtype),
5155+
DataFrame(columns=['A'], dtype=dtype),
5156+
DataFrame(index=[0], dtype=dtype),
5157+
]
5158+
for df in frames:
5159+
self.assertTrue((df + df).equals(df))
5160+
assert_frame_equal(df + df, df)
5161+
51515162
def test_ops_np_scalar(self):
51525163
vals, xs = np.random.rand(5, 3), [nan, 7, -23, 2.718, -3.14, np.inf]
51535164
f = lambda x: DataFrame(x, index=list('ABCDE'),

0 commit comments

Comments
 (0)