Skip to content

BUG: Concat with inner join and empty DataFrame #15397

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ Bug Fixes
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)

- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)

- Bug in ``pd.concat()`` in which empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)



Expand All @@ -579,4 +579,4 @@ Bug Fixes
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
10 changes: 10 additions & 0 deletions pandas/tests/tools/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,16 @@ def test_concat_bug_3602(self):
result = concat([df1, df2], axis=1)
assert_frame_equal(result, expected)

def test_concat_inner_join_empty(self):
# GH 15328
df_empty = pd.DataFrame()
df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1], dtype='int64')
df_expected = pd.DataFrame({'a': []}, index=[], dtype='int64')

for how, expected in [('inner', df_expected), ('outer', df_a)]:
Copy link
Contributor

Choose a reason for hiding this comment

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

right forgot this only accepts outer & inner

result = pd.concat([df_a, df_empty], axis=1, join=how)
assert_frame_equal(result, expected)

def test_concat_series_axis1_same_names_ignore_index(self):
dates = date_range('01-Jan-2013', '01-Jan-2014', freq='MS')[0:-1]
s1 = Series(randn(len(dates)), index=dates, name='value')
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/tools/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def setUp(self):
self.right = DataFrame({'v2': np.random.randn(4)},
index=['d', 'b', 'c', 'a'])

def test_merge_inner_join_empty(self):
# GH 15328
df_empty = pd.DataFrame()
df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1], dtype='int64')
result = pd.merge(df_empty, df_a, left_index=True, right_index=True)
expected = pd.DataFrame({'a': []}, index=[], dtype='int64')
assert_frame_equal(result, expected)

def test_merge_common(self):
joined = merge(self.df, self.df2)
exp = merge(self.df, self.df2, on=['key1', 'key2'])
Expand Down
4 changes: 3 additions & 1 deletion pandas/tools/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
if sum(obj.shape) > 0 or isinstance(obj, Series)]

if (len(non_empties) and (keys is None and names is None and
levels is None and join_axes is None)):
levels is None and
join_axes is None and
not self.intersect)):
objs = non_empties
sample = objs[0]

Expand Down