diff --git a/doc/source/whatsnew/v0.16.2.txt b/doc/source/whatsnew/v0.16.2.txt index 3665948d15271..48fce87f5088d 100644 --- a/doc/source/whatsnew/v0.16.2.txt +++ b/doc/source/whatsnew/v0.16.2.txt @@ -41,6 +41,8 @@ Other API Changes - ``Holiday`` now raises ``NotImplementedError`` if both ``offset`` and ``observance`` are used in constructor instead of returning an incorrect result (:issue:`10217`). +- Adding empty ``DataFrame``s results in a ``DataFrame`` that ``.equals`` an empty ``DataFrame`` (:issue:`10181`) + .. _whatsnew_0162.performance: Performance Improvements diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 3395ea360165e..8ff39f4fb0e06 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3530,11 +3530,15 @@ def construction_error(tot_items, block_shape, axes, e=None): def create_block_manager_from_blocks(blocks, axes): try: if len(blocks) == 1 and not isinstance(blocks[0], Block): - # It's OK if a single block is passed as values, its placement is - # basically "all items", but if there're many, don't bother - # converting, it's an error anyway. - blocks = [make_block(values=blocks[0], - placement=slice(0, len(axes[0])))] + # if blocks[0] is of length 0, return empty blocks + if not len(blocks[0]): + blocks = [] + else: + # It's OK if a single block is passed as values, its placement is + # basically "all items", but if there're many, don't bother + # converting, it's an error anyway. + blocks = [make_block(values=blocks[0], + placement=slice(0, len(axes[0])))] mgr = BlockManager(blocks, axes) mgr._consolidate_inplace() diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index f74cb07557342..a6fafb445925b 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -5142,6 +5142,17 @@ def test_operators(self): df = DataFrame({'a': ['a', None, 'b']}) assert_frame_equal(df + df, DataFrame({'a': ['aa', np.nan, 'bb']})) + # Test for issue #10181 + for dtype in ('float', 'int64'): + frames = [ + DataFrame(dtype=dtype), + DataFrame(columns=['A'], dtype=dtype), + DataFrame(index=[0], dtype=dtype), + ] + for df in frames: + self.assertTrue((df + df).equals(df)) + assert_frame_equal(df + df, df) + def test_ops_np_scalar(self): vals, xs = np.random.rand(5, 3), [nan, 7, -23, 2.718, -3.14, np.inf] f = lambda x: DataFrame(x, index=list('ABCDE'),