Skip to content

BUG: fixed merging with empty frame containing an Int64 column (#25183) #25289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Bug Fixes
**Other**

- Bug in :meth:`Series.is_unique` where single occurrences of ``NaN`` were not considered unique (:issue:`25180`)
-
- Bug in :func:`merge` when merging an empty ``DataFrame`` with an ``Int64`` column or a non-empty ``DataFrame`` with an ``Int64`` column that is all ``NaN`` (:issue:`25183`)
-

.. _whatsnew_0.242.contributors:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def get_reindexed_values(self, empty_dtype, upcasted_na):
pass
elif getattr(self.block, 'is_sparse', False):
pass
elif getattr(self.block, 'is_extension', False):
pass
else:
missing_arr = np.empty(self.shape, dtype=empty_dtype)
missing_arr.fill(fill_value)
Expand Down
59 changes: 59 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections import OrderedDict
from datetime import date, datetime
import itertools
import random
import re

Expand Down Expand Up @@ -428,6 +429,64 @@ def check2(exp, kwarg):
check1(exp_in, kwarg)
check2(exp_out, kwarg)

@pytest.mark.parametrize(
'join_col, val_col', list(itertools.product([
pd.Series([1], dtype='int64'),
pd.Series([1], dtype='Int64'),
pd.Series([1.23]),
pd.Series(['foo']),
pd.Series([True]),
pd.Series([pd.Timestamp('2018-01-01')]),
pd.Series([pd.Timestamp('2018-01-01', tz='US/Eastern')]),
], repeat=2)),
ids=lambda x: x.dtype.name
)
def test_merge_empty_frame(self, join_col, val_col):
# GH 25183
df = pd.DataFrame({'a': join_col, 'b': val_col}, columns=['a', 'b'])
df_empty = df[:0]
exp = pd.DataFrame({
'b_x': pd.Series(dtype=df.dtypes['b']),
'a': pd.Series(dtype=df.dtypes['a']),
'b_y': pd.Series(dtype=df.dtypes['b']),
}, columns=['b_x', 'a', 'b_y'])
act = df_empty.merge(df, on='a')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use result and expected here rather than abbrevs

assert_frame_equal(act, exp)

@pytest.mark.parametrize(
'join_col, val_col', list(itertools.product(
[
pd.Series([1], dtype='int64'),
pd.Series([1], dtype='Int64'),
pd.Series([1.23]),
pd.Series(['foo']),
pd.Series([True]),
pd.Series([pd.Timestamp('2018-01-01')]),
pd.Series([pd.Timestamp('2018-01-01', tz='US/Eastern')]),
],
[
pd.Series([np.nan], dtype='Int64'),
pd.Series([np.nan], dtype='float'),
pd.Series([np.nan], dtype='object'),
pd.Series([pd.NaT]),
]
)),
ids=lambda x: x.dtype.name
)
def test_merge_all_na_column(self, join_col, val_col):
# GH 25183
df_left = pd.DataFrame(
{'a': join_col, 'b': val_col}, columns=['a', 'b'])
df_right = pd.DataFrame(
{'a': join_col, 'b': val_col}, columns=['a', 'b'])
exp = pd.DataFrame({
'a': join_col,
'b_x': val_col,
'b_y': val_col,
}, columns=['a', 'b_x', 'b_y'])
act = df_left.merge(df_right, on='a')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

assert_frame_equal(act, exp)

def test_merge_nosort(self):
# #2098, anything to do?

Expand Down