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

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

Comments

Projects
None yet
4 participants

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

Contributor

benjello commented Jan 18, 2016

👍

Contributor

jreback commented Jan 19, 2016

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

jreback added this to the Next Major Release milestone Jan 19, 2016

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'
Contributor

jreback commented Jan 20, 2016

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

dan-tee commented Jan 21, 2016

I'd love to give it a shot.

dan-tee commented Jan 21, 2016

I've opened #12112

@jreback jreback modified the milestone: 0.18.0, Next Major Release Jan 22, 2016

jreback closed this in 5641665 Jan 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment