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 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: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,5 @@ Bug Fixes
- 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`)
Copy link
Contributor

Choose a reason for hiding this comment

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

just an FYI, if you put changes in whatsnew NOT at the end (but in empty space that is left), then you won't get conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah i see. makes sense. thanks!


- Bug in ``pd.concat()`` in which empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
9 changes: 9 additions & 0 deletions pandas/tests/tools/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,15 @@ def test_concat_bug_3602(self):
result = concat([df1, df2], axis=1)
assert_frame_equal(result, expected)

def test_concat_bug_15328(self):
df_empty = pd.DataFrame()
Copy link
Contributor

Choose a reason for hiding this comment

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

put a comment here for the issue number (rather than in the test name) (try to be descriptive with that if possible)

df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1])
result = pd.concat([df_empty, df_a], axis=1, join='inner')
self.assertTrue(result.empty)
Copy link
Contributor

Choose a reason for hiding this comment

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

construct the result frame and use assert_frame_equal


result = pd.concat([df_a, df_empty], axis=1, join='inner')
self.assertTrue(result.empty)
Copy link
Contributor

Choose a reason for hiding this comment

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

same


def test_concat_series_axis1_same_names_ignore_index(self):
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 systematically tests the how=* here? (for the empty cases)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry i'm not clear on what you mean here. could you clarify our point to something similar ?

Copy link
Contributor

@jreback jreback Feb 14, 2017

Choose a reason for hiding this comment

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

what I mean is something like:

for how, expected in [('inner', df_empty), ('outer', df_a), ('left', df_a), ('right', df_empty)]:
    result = pd.concat([df_a, df_empty, axis=1, join=how)
    assert_frame_equal(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh haha. got it. thanks!

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

def test_merge_bug_15328(self):
df_empty = pd.DataFrame()
df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1])
result = pd.merge(df_empty, df_a, left_index=True, right_index=True)
self.assertTrue(result.empty)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


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