Skip to content

Commit

Permalink
[Backport #14434] ERR: Checks for left_index and right_index merge pa…
Browse files Browse the repository at this point in the history
…rameters

Author: Iván Vallés Pérez <ivanvallesperez@gmail.com>

Closes #14434 from ivallesp/add-check-for-merge-indices and squashes the following commits:

e18b7c9 [Iván Vallés Pérez] Add some checks for assuring that the left_index and right_index parameters have correct types. Tests added.

(cherry picked from commit 2d3a739)
  • Loading branch information
ivallesp authored and jorisvandenbossche committed Nov 1, 2016
1 parent 9857248 commit b0e4589
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Performance Improvements




.. _whatsnew_0191.bug_fixes:

Bug Fixes
Expand All @@ -42,6 +43,9 @@ Bug Fixes



- ``pd.merge()`` will raise ``ValueError`` with non-boolean parameters in passed boolean type arguments (:issue:`14434`)



- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``'rows'`` or ``'columns'`` (:issue:`14369`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ def __init__(self, left, right, how='inner', on=None,
'can not merge DataFrame with instance of '
'type {0}'.format(type(right)))

if not is_bool(left_index):
raise ValueError(
'left_index parameter must be of type bool, not '
'{0}'.format(type(left_index)))
if not is_bool(right_index):
raise ValueError(
'right_index parameter must be of type bool, not '
'{0}'.format(type(right_index)))

# warn user when merging between different levels
if left.columns.nlevels != right.columns.nlevels:
msg = ('merging between different levels can give an unintended '
Expand Down
9 changes: 9 additions & 0 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ def test_merge_misspecified(self):
self.assertRaises(ValueError, merge, self.df, self.df2,
left_on=['key1'], right_on=['key1', 'key2'])

def test_index_and_on_parameters_confusion(self):
self.assertRaises(ValueError, merge, self.df, self.df2, how='left',
left_index=False, right_index=['key1', 'key2'])
self.assertRaises(ValueError, merge, self.df, self.df2, how='left',
left_index=['key1', 'key2'], right_index=False)
self.assertRaises(ValueError, merge, self.df, self.df2, how='left',
left_index=['key1', 'key2'],
right_index=['key1', 'key2'])

def test_merge_overlap(self):
merged = merge(self.left, self.left, on='key')
exp_len = (self.left['key'].value_counts() ** 2).sum()
Expand Down

0 comments on commit b0e4589

Please sign in to comment.