Skip to content

Commit c7300ea

Browse files
abaldenkojreback
authored andcommitted
BUG: Concat with inner join and empty DataFrame
closes #15328 Author: abaldenko <[email protected]> Closes #15397 from abaldenko/concat_empty_dataframe and squashes the following commits: 47c8735 [abaldenko] BUG: Concat with inner join and empty DataFrame fc473b7 [abaldenko] BUG: Concat with inner join and empty DataFrame b86dcb6 [abaldenko] BUG: Concat with inner join and empty DataFrame
1 parent 5a8883b commit c7300ea

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Bug Fixes
576576
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)
577577

578578
- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)
579-
579+
- Bug in ``pd.concat()`` in which concatting with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
580580

581581

582582

pandas/tests/tools/test_concat.py

+10
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,16 @@ def test_concat_bug_3602(self):
18251825
result = concat([df1, df2], axis=1)
18261826
assert_frame_equal(result, expected)
18271827

1828+
def test_concat_inner_join_empty(self):
1829+
# GH 15328
1830+
df_empty = pd.DataFrame()
1831+
df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1], dtype='int64')
1832+
df_expected = pd.DataFrame({'a': []}, index=[], dtype='int64')
1833+
1834+
for how, expected in [('inner', df_expected), ('outer', df_a)]:
1835+
result = pd.concat([df_a, df_empty], axis=1, join=how)
1836+
assert_frame_equal(result, expected)
1837+
18281838
def test_concat_series_axis1_same_names_ignore_index(self):
18291839
dates = date_range('01-Jan-2013', '01-Jan-2014', freq='MS')[0:-1]
18301840
s1 = Series(randn(len(dates)), index=dates, name='value')

pandas/tests/tools/test_merge.py

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def setUp(self):
5252
self.right = DataFrame({'v2': np.random.randn(4)},
5353
index=['d', 'b', 'c', 'a'])
5454

55+
def test_merge_inner_join_empty(self):
56+
# GH 15328
57+
df_empty = pd.DataFrame()
58+
df_a = pd.DataFrame({'a': [1, 2]}, index=[0, 1], dtype='int64')
59+
result = pd.merge(df_empty, df_a, left_index=True, right_index=True)
60+
expected = pd.DataFrame({'a': []}, index=[], dtype='int64')
61+
assert_frame_equal(result, expected)
62+
5563
def test_merge_common(self):
5664
joined = merge(self.df, self.df2)
5765
exp = merge(self.df, self.df2, on=['key1', 'key2'])

pandas/tools/concat.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
284284
if sum(obj.shape) > 0 or isinstance(obj, Series)]
285285

286286
if (len(non_empties) and (keys is None and names is None and
287-
levels is None and join_axes is None)):
287+
levels is None and
288+
join_axes is None and
289+
not self.intersect)):
288290
objs = non_empties
289291
sample = objs[0]
290292

0 commit comments

Comments
 (0)