Skip to content
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

Merge DataFrame and Series using on #21220

Closed
TomAugspurger opened this issue May 26, 2018 · 0 comments · Fixed by #21223
Closed

Merge DataFrame and Series using on #21220

TomAugspurger opened this issue May 26, 2018 · 0 comments · Fixed by #21223
Labels
API Design Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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
@TomAugspurger TomAugspurger added Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design labels May 26, 2018
@TomAugspurger TomAugspurger added this to the Next Major Release milestone May 26, 2018
@TomAugspurger TomAugspurger mentioned this issue May 26, 2018
4 tasks
@jorisvandenbossche jorisvandenbossche modified the milestones: Contributions Welcome, 0.24.0 Jul 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants