Skip to content

BUG: numericlike set ops on unsupported Indexes #10042

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

Merged
merged 2 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,17 +1179,18 @@ def argsort(self, *args, **kwargs):
return result.argsort(*args, **kwargs)

def __add__(self, other):
if isinstance(other, Index):
if com.is_list_like(other):
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
"use '|' or .union()",FutureWarning)
if isinstance(other, Index):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct I think, as now a list will show the warning, but not perform a set (union) operation (as a list is not an index)?

Should it not be like:

if com.is_list_like(other):
    warnings.warn(...)
    return self.union(other)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I don't really see how this is fixed? It is still

if com.is_list_like(other):
    warnings.warn(...)
if isinstance(other, Index):
    return self.union(other)

So a list will trigger the warning, but not do the union set operation.

return self.union(other)
return Index(np.array(self) + other)
__iadd__ = __add__
__radd__ = __add__

def __sub__(self, other):
if isinstance(other, Index):
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
"use .difference()",FutureWarning)
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
"use .difference()",FutureWarning)
return self.difference(other)

def __and__(self, other):
Expand Down Expand Up @@ -2469,6 +2470,21 @@ def _evaluate_compare(self, other):
cls.__le__ = _make_compare('__le__')
cls.__ge__ = _make_compare('__ge__')

@classmethod
def _add_numericlike_set_methods_disabled(cls):
""" add in the numeric set-like methods to disable """

def _make_invalid_op(name):

def invalid_op(self, other=None):
raise TypeError("cannot perform {name} with this index type: {typ}".format(name=name,
typ=type(self)))
invalid_op.__name__ = name
return invalid_op

cls.__add__ = cls.__radd__ = __iadd__ = _make_invalid_op('__add__')
cls.__sub__ = __isub__ = _make_invalid_op('__sub__')

@classmethod
def _add_numeric_methods_disabled(cls):
""" add in numeric methods to disable """
Expand Down Expand Up @@ -3148,6 +3164,7 @@ def _add_accessors(cls):
overwrite=True)


CategoricalIndex._add_numericlike_set_methods_disabled()
CategoricalIndex._add_numeric_methods_disabled()
CategoricalIndex._add_logical_methods_disabled()
CategoricalIndex._add_comparison_methods()
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def _convert_level_number(level_num, columns):
new_data[key] = value_slice.ravel()

if len(drop_cols) > 0:
new_columns = new_columns - drop_cols
new_columns = new_columns.difference(drop_cols)

N = len(this)

Expand Down Expand Up @@ -1045,7 +1045,7 @@ def check_len(item, name):
with_dummies = [result]
for (col, pre, sep) in zip(columns_to_encode, prefix, prefix_sep):

dummy = _get_dummies_1d(data[col], prefix=pre, prefix_sep=sep,
dummy = _get_dummies_1d(data[col], prefix=pre, prefix_sep=sep,
dummy_na=dummy_na, sparse=sparse)
with_dummies.append(dummy)
result = concat(with_dummies, axis=1)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3613,7 +3613,7 @@ def test_frame_select_complex(self):

# invert ok for filters
result = store.select('df', "~(columns=['A','B'])")
expected = df.loc[:,df.columns-['A','B']]
expected = df.loc[:,df.columns.difference(['A','B'])]
tm.assert_frame_equal(result, expected)

# in
Expand Down
26 changes: 25 additions & 1 deletion pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@ def test_add(self):
# - API change GH 8226
with tm.assert_produces_warning():
self.strIndex + self.strIndex
with tm.assert_produces_warning():
self.strIndex + self.strIndex.tolist()
with tm.assert_produces_warning():
self.strIndex.tolist() + self.strIndex

firstCat = self.strIndex.union(self.dateIndex)
secondCat = self.strIndex.union(self.strIndex)
Expand Down Expand Up @@ -772,6 +776,7 @@ def test_difference(self):
assertRaisesRegexp(TypeError, "iterable", first.difference, 0.5)

def test_symmetric_diff(self):

# smoke
idx1 = Index([1, 2, 3, 4], name='idx1')
idx2 = Index([2, 3, 4, 5])
Expand Down Expand Up @@ -819,7 +824,7 @@ def test_symmetric_diff(self):

# other isn't iterable
with tm.assertRaises(TypeError):
Index(idx1,dtype='object') - 1
Index(idx1,dtype='object').difference(1)

def test_is_numeric(self):
self.assertFalse(self.dateIndex.is_numeric())
Expand Down Expand Up @@ -1488,6 +1493,19 @@ def test_construction_with_dtype(self):
result = CategoricalIndex(idx, categories=idx, ordered=True)
tm.assert_index_equal(result, expected, exact=True)

def test_disallow_set_ops(self):

# GH 10039
# set ops (+/-) raise TypeError
idx = pd.Index(pd.Categorical(['a', 'b']))

self.assertRaises(TypeError, lambda : idx - idx)
self.assertRaises(TypeError, lambda : idx + idx)
self.assertRaises(TypeError, lambda : idx - ['a','b'])
self.assertRaises(TypeError, lambda : idx + ['a','b'])
self.assertRaises(TypeError, lambda : ['a','b'] - idx)
self.assertRaises(TypeError, lambda : ['a','b'] + idx)

def test_method_delegation(self):

ci = CategoricalIndex(list('aabbca'), categories=list('cabdef'))
Expand Down Expand Up @@ -3882,6 +3900,12 @@ def test_difference(self):
# - API change GH 8226
with tm.assert_produces_warning():
first - self.index[-3:]
with tm.assert_produces_warning():
self.index[-3:] - first
with tm.assert_produces_warning():
self.index[-3:] - first.tolist()

self.assertRaises(TypeError, lambda : first.tolist() - self.index[-3:])

expected = MultiIndex.from_tuples(sorted(self.index[:-3].values),
sortorder=0,
Expand Down