Skip to content

Commit

Permalink
Revert new warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Aug 23, 2018
1 parent d2f1e78 commit e1f474e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
21 changes: 1 addition & 20 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3862,29 +3862,10 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
-------
dataframe : DataFrame
"""
from pandas import Series

inplace = validate_bool_kwarg(inplace, 'inplace')
if not isinstance(keys, list):
keys = [keys]

# collect elements from "keys" that are not allowed array types
col_labels = [x for x in keys
if not isinstance(x, (Series, Index, MultiIndex,
list, np.ndarray))]
if any(x not in self for x in col_labels):
# if there are any labels that are invalid, we raise a KeyError
missing = [x for x in col_labels if x not in self]
raise KeyError('{}'.format(missing))

elif len(set(col_labels)) < len(col_labels):
# if all are valid labels, but there are duplicates
dup = Series(col_labels)
dup = list(dup.loc[dup.duplicated()])
raise ValueError('Passed duplicate column names '
'to keys: {dup}'.format(dup=dup))

inplace = validate_bool_kwarg(inplace, 'inplace')

if inplace:
frame = self
else:
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ def test_set_index_pass_arrays_duplicate(self, frame_of_index_cols, drop,
keys = [box1(df['A']), box2(df['A'])]

# == gives ambiguous Boolean for Series
if keys[0] is 'A' and keys[1] is 'A':
with tm.assert_raises_regex(ValueError,
'Passed duplicate column names.*'):
if drop and keys[0] is 'A' and keys[1] is 'A':
with tm.assert_raises_regex(KeyError, '.*'):
df.set_index(keys, drop=drop, append=append)
else:
result = df.set_index(keys, drop=drop, append=append)
Expand Down Expand Up @@ -225,15 +224,17 @@ def test_set_index_verify_integrity(self, frame_of_index_cols):
'Index has duplicate keys'):
df.set_index([df['A'], df['A']], verify_integrity=True)

def test_set_index_raise(self, frame_of_index_cols):
@pytest.mark.parametrize('append', [True, False])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_raise(self, frame_of_index_cols, drop, append):
df = frame_of_index_cols

with tm.assert_raises_regex(KeyError, '.*'): # column names are A-E
df.set_index(['foo', 'bar', 'baz'], verify_integrity=True)
df.set_index(['foo', 'bar', 'baz'], drop=drop, append=append)

# non-existent key in list with arrays
with tm.assert_raises_regex(KeyError, '.*'):
df.set_index([df['A'], df['B'], 'X'], verify_integrity=True)
df.set_index([df['A'], df['B'], 'X'], drop=drop, append=append)

def test_construction_with_categorical_index(self):
ci = tm.makeCategoricalIndex(10)
Expand Down

0 comments on commit e1f474e

Please sign in to comment.