Skip to content

Commit b0c7806

Browse files
committed
BUG: Allow merging on Index vectors
This behavior used to work in v0.19.0 and is consistent with the documentation. Closes pandas-devgh-19038
1 parent 9303315 commit b0c7806

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Reshaping
373373
- Bug in :func:`Series.rank` where ``Series`` containing ``NaT`` modifies the ``Series`` inplace (:issue:`18521`)
374374
- Bug in :func:`cut` which fails when using readonly arrays (:issue:`18773`)
375375
- Bug in :func:`Dataframe.pivot_table` which fails when the ``aggfunc`` arg is of type string. The behavior is now consistent with other methods like ``agg`` and ``apply`` (:issue:`18713`)
376-
376+
- Bug in :func:`DataFrame.merge` in which merging using ``Index`` objects as vectors raised an Exception (:issue:`19038`)
377377

378378
Numeric
379379
^^^^^^^

pandas/core/reshape/merge.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,12 +814,12 @@ def _get_merge_keys(self):
814814
join_names = []
815815
right_drop = []
816816
left_drop = []
817+
817818
left, right = self.left, self.right
819+
list_types = (np.ndarray, Series, Index)
818820

819-
is_lkey = lambda x: isinstance(
820-
x, (np.ndarray, Series)) and len(x) == len(left)
821-
is_rkey = lambda x: isinstance(
822-
x, (np.ndarray, Series)) and len(x) == len(right)
821+
is_lkey = lambda x: isinstance(x, list_types) and len(x) == len(left)
822+
is_rkey = lambda x: isinstance(x, list_types) and len(x) == len(right)
823823

824824
# Note that pd.merge_asof() has separate 'on' and 'by' parameters. A
825825
# user could, for example, request 'left_index' and 'left_by'. In a

pandas/tests/reshape/merge/test_merge.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,22 @@ def f():
13701370
household.join(log_return, how='outer')
13711371
pytest.raises(NotImplementedError, f)
13721372

1373+
def test_merge_datetime_index(self):
1374+
# see gh-19038
1375+
df = DataFrame([1, 2, 3],
1376+
["2016-01-01", "2017-01-01", "2018-01-01"],
1377+
columns=["a"])
1378+
df.index = pd.to_datetime(df.index)
1379+
1380+
expected = DataFrame({"a": [1, 2, 3]})
1381+
result = df.merge(df, on=["a", df.index.year], how="inner")
1382+
tm.assert_frame_equal(result, expected)
1383+
1384+
expected = DataFrame({"a_x": [1, 2, 3],
1385+
"a_y": [1, 2, 3]})
1386+
result = df.merge(df, on=[df.index.year], how="inner")
1387+
tm.assert_frame_equal(result, expected)
1388+
13731389

13741390
class TestMergeDtypes(object):
13751391

0 commit comments

Comments
 (0)