Skip to content

DataFrame.merge with Series should give nice error message #12081

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

Closed
dan-tee opened this issue Jan 18, 2016 · 6 comments
Closed

DataFrame.merge with Series should give nice error message #12081

dan-tee opened this issue Jan 18, 2016 · 6 comments
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@dan-tee
Copy link

dan-tee commented Jan 18, 2016

Right now trying this results in "IndexError: list index out of range". It should say can't merge DataFrame with a Series...

I know this for quite a while now, but still get trapped on it every once in a while. This would be very helpful for beginners.

Other people also get confused: http://stackoverflow.com/questions/27281734/pandas-merge-on-index-not-working

@benjello
Copy link
Contributor

👍

@jreback
Copy link
Contributor

jreback commented Jan 19, 2016

can you post a copy-pastable example that shows the error message.

@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Error Reporting Incorrect or improved errors from pandas labels Jan 19, 2016
@jreback jreback added this to the Next Major Release milestone Jan 19, 2016
@halflings
Copy link

import pandas as pd
df = pd.DataFrame(dict(a=range(20)))
pd.merge(df, df.a, left_index=True, right_index=True)

This gives:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-fae8bf77732c> in <module>()
      1 import pandas as pd
      2 df = pd.DataFrame(dict(a=range(20)))
----> 3 pd.merge(df, df.a, left_index=True, right_index=True)

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
     33                          right_index=right_index, sort=sort, suffixes=suffixes,
     34                          copy=copy, indicator=indicator)
---> 35     return op.get_result()
     36 if __debug__:
     37     merge.__doc__ = _merge_doc % '\nleft : DataFrame'

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in get_result(self)
    194             self.left, self.right = self._indicator_pre_merge(self.left, self.right)
    195
--> 196         join_index, left_indexer, right_indexer = self._get_join_info()
    197
    198         ldata, rdata = self.left._data, self.right._data

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in _get_join_info(self)
    304     def _get_join_info(self):
    305         left_ax = self.left._data.axes[self.axis]
--> 306         right_ax = self.right._data.axes[self.axis]
    307
    308         if self.left_index and self.right_index:

IndexError: list index out of range

And then there's this as well:

In [10]: pd.merge(df, df.a)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-dba79c04ae03> in <module>()
----> 1 pd.merge(df, df.a)

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
     32                          right_on=right_on, left_index=left_index,
     33                          right_index=right_index, sort=sort, suffixes=suffixes,
---> 34                          copy=copy, indicator=indicator)
     35     return op.get_result()
     36 if __debug__:

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator)
    188         (self.left_join_keys,
    189          self.right_join_keys,
--> 190          self.join_names) = self._get_merge_keys()
    191
    192     def get_result(self):

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in _get_merge_keys(self)
    376         left_keys, right_keys
    377         """
--> 378         self._validate_specification()
    379
    380         left_keys = []

/usr/local/lib/python3.5/site-packages/pandas/tools/merge.py in _validate_specification(self)
    465                 # use the common columns
    466                 common_cols = self.left.columns.intersection(
--> 467                     self.right.columns)
    468                 if len(common_cols) == 0:
    469                     raise MergeError('No common columns to perform merge on')

/usr/local/lib/python3.5/site-packages/pandas/core/generic.py in __getattr__(self, name)
   2393                 or name in self._metadata
   2394                 or name in self._accessors):
-> 2395             return object.__getattribute__(self, name)
   2396         else:
   2397             if name in self._info_axis:

AttributeError: 'Series' object has no attribute 'columns'

@jreback
Copy link
Contributor

jreback commented Jan 20, 2016

thanks!. yes these could use more intuitive warnings. want to do a PR?

@dan-tee
Copy link
Author

dan-tee commented Jan 21, 2016

I'd love to give it a shot.

@dan-tee
Copy link
Author

dan-tee commented Jan 21, 2016

I've opened #12112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants