Skip to content

Commit cb91df3

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 c0e3767 commit cb91df3

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ Reshaping
407407
- Bug in :func:`Series.rank` where ``Series`` containing ``NaT`` modifies the ``Series`` inplace (:issue:`18521`)
408408
- Bug in :func:`cut` which fails when using readonly arrays (:issue:`18773`)
409409
- 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`)
410-
410+
- Bug in :func:`DataFrame.merge` in which merging using ``Index`` objects as vectors raised an Exception (:issue:`19038`)
411411

412412
Numeric
413413
^^^^^^^

pandas/core/reshape/merge.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,13 @@ def _get_merge_keys(self):
814814
join_names = []
815815
right_drop = []
816816
left_drop = []
817+
817818
left, right = self.left, self.right
818819
stacklevel = 5 # Number of stack levels from df.merge
820+
list_types = (np.ndarray, Series, Index)
819821

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

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

pandas/tests/reshape/merge/test_merge.py

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

1373+
@pytest.mark.parametrize("klass", [None, np.asarray, Series, Index])
1374+
def test_merge_datetime_index(self, klass):
1375+
# see gh-19038
1376+
df = DataFrame([1, 2, 3],
1377+
["2016-01-01", "2017-01-01", "2018-01-01"],
1378+
columns=["a"])
1379+
df.index = pd.to_datetime(df.index)
1380+
on_vector = df.index.year
1381+
1382+
if klass is not None:
1383+
on_vector = klass(on_vector)
1384+
1385+
expected = DataFrame({"a": [1, 2, 3]})
1386+
1387+
if klass == np.asarray:
1388+
# The join key is added for ndarray.
1389+
expected["key_1"] = [2016, 2017, 2018]
1390+
1391+
result = df.merge(df, on=["a", on_vector], how="inner")
1392+
tm.assert_frame_equal(result, expected)
1393+
1394+
expected = DataFrame({"a_x": [1, 2, 3],
1395+
"a_y": [1, 2, 3]})
1396+
result = df.merge(df, on=[df.index.year], how="inner")
1397+
tm.assert_frame_equal(result, expected)
1398+
13731399

13741400
class TestMergeDtypes(object):
13751401

0 commit comments

Comments
 (0)