Skip to content

Merge DataFrame and Series using on #21220

@TomAugspurger

Description

@TomAugspurger

Now that we support merging on combination of column names and index levels, this should work

In [40]: a = pd.DataFrame({"A": [1, 2, 3, 4]}, index=pd.MultiIndex.from_product([['a', 'b'], [0, 1]], names=['outer', 'inner'])) In [41]: b = pd.Series([1, 2, 3, 4], index=pd.MultiIndex.from_product([['a', 'b'], [1, 2]], names=['outer', 'inner']), name='B') In [42]: pd.merge(a, b, on=['outer', 'inner']) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-42-337c5a9e9f8f> in <module>() ----> 1 pd.merge(a, b, on=['outer', 'inner']) ~/Envs/dask-dev/lib/python3.6/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 58 right_index=right_index, sort=sort, suffixes=suffixes, 59 copy=copy, indicator=indicator, ---> 60 validate=validate) 61 return op.get_result() 62 ~/Envs/dask-dev/lib/python3.6/site-packages/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate) 524 if not isinstance(right, DataFrame): 525 raise ValueError('can not merge DataFrame with instance of ' --> 526 'type {right}'.format(right=type(right))) 527 528 if not is_bool(left_index): ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

Should be the same as

In [39]: pd.merge(a, b.to_frame(), on=['outer', 'inner']) Out[39]: A B outer inner a 1 2 1 b 1 4 3

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions