-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Index.difference of itself doesn't preserve type #20062
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
from pandas import (period_range, date_range, Series, | ||
DataFrame, Float64Index, Int64Index, UInt64Index, | ||
CategoricalIndex, DatetimeIndex, TimedeltaIndex, | ||
PeriodIndex, isna) | ||
PeriodIndex, RangeIndex, isna) | ||
from pandas.core.index import _get_combined_index, _ensure_index_from_sequences | ||
from pandas.util.testing import assert_almost_equal | ||
from pandas.compat.numpy import np_datetime64_compat | ||
|
@@ -44,7 +44,7 @@ def setup_method(self, method): | |
tdIndex=tm.makeTimedeltaIndex(100), | ||
intIndex=tm.makeIntIndex(100), | ||
uintIndex=tm.makeUIntIndex(100), | ||
rangeIndex=tm.makeIntIndex(100), | ||
rangeIndex=tm.makeRangeIndex(100), | ||
floatIndex=tm.makeFloatIndex(100), | ||
boolIndex=Index([True, False]), | ||
catIndex=tm.makeCategoricalIndex(100), | ||
|
@@ -57,6 +57,15 @@ def setup_method(self, method): | |
def create_index(self): | ||
return Index(list('abcde')) | ||
|
||
def generate_index_types(self, skip_index_keys=[]): | ||
""" | ||
Return a generator of the various index types, leaving | ||
out the ones with a key in skip_index_keys | ||
""" | ||
for key, idx in self.indices.items(): | ||
if key not in skip_index_keys: | ||
yield key, idx | ||
|
||
def test_new_axis(self): | ||
new_index = self.dateIndex[None, :] | ||
assert new_index.ndim == 2 | ||
|
@@ -406,6 +415,27 @@ def test_constructor_dtypes_timedelta(self): | |
pd.TimedeltaIndex(list(values), dtype=dtype)]: | ||
tm.assert_index_equal(res, idx) | ||
|
||
def test_constructor_empty(self): | ||
skip_index_keys = ["repeats", "periodIndex", "rangeIndex", | ||
"tuples"] | ||
for key, idx in self.generate_index_types(skip_index_keys): | ||
empty = idx.__class__([]) | ||
assert isinstance(empty, idx.__class__) | ||
assert not len(empty) | ||
|
||
empty = PeriodIndex([], freq='B') | ||
assert isinstance(empty, PeriodIndex) | ||
assert not len(empty) | ||
|
||
empty = RangeIndex(step=1) | ||
assert isinstance(empty, pd.RangeIndex) | ||
assert not len(empty) | ||
|
||
empty = MultiIndex(levels=[[1, 2], ['blue', 'red']], | ||
labels=[[], []]) | ||
assert isinstance(empty, MultiIndex) | ||
assert not len(empty) | ||
|
||
def test_view_with_args(self): | ||
|
||
restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex', | ||
|
@@ -1034,6 +1064,27 @@ def test_symmetric_difference(self): | |
assert tm.equalContents(result, expected) | ||
assert result.name == 'new_name' | ||
|
||
def test_difference_type(self): | ||
# GH 20040 | ||
# If taking difference of a set and itself, it | ||
# needs to preserve the type of the index | ||
skip_index_keys = ['repeats'] | ||
for key, idx in self.generate_index_types(skip_index_keys): | ||
result = idx.difference(idx) | ||
expected = idx.drop(idx) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_intersection_difference(self): | ||
# GH 20040 | ||
# Test that the intersection of an index with an | ||
# empty index produces the same index as the difference | ||
# of an index with itself. Test for all types | ||
skip_index_keys = ['repeats'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for followup PR (or can change here if want to update where needed). a function to return yeld the key, idx while filtering on certain types would be great. (like what you are doing but in a module level function) |
||
for key, idx in self.generate_index_types(skip_index_keys): | ||
inter = idx.intersection(idx.drop(idx)) | ||
diff = idx.difference(idx) | ||
tm.assert_index_equal(inter, diff) | ||
|
||
def test_is_numeric(self): | ||
assert not self.dateIndex.is_numeric() | ||
assert not self.strIndex.is_numeric() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a test which tries to construct all indices as empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback for a test to construct all indices as empty, is this a test of the public API's (which have to differ based on the specific index class), or a test of the private
Index._shallow_copy([])
?For example,
pd.RangeIndex([])
currently fails withTypeError: RangeIndex(...) must be called with integers, list was passed for start
, which is correct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right you would skip that one. I think there is already a test of construct empty (pretty sure), but make just point it out and make it IS testing everything.