-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
TST: Reorganize IntervalIndex tests #28800
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64a2550
rename test_interval_new.py -> test_indexing.py
jschendel af11031
move tests to/from test_indexing.py
jschendel cfeb61a
separate classes and reorder test_indexing.py
jschendel ea93c0e
create test_base.py for common base tests
jschendel 37a08a6
create test_formats.py and move tests
jschendel 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import IntervalIndex, Series, date_range | ||
from pandas.tests.indexes.common import Base | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestBase(Base): | ||
""" | ||
Tests specific to the shared common index tests; unrelated tests should be placed | ||
in test_interval.py or the specific test file (e.g. test_astype.py) | ||
""" | ||
|
||
_holder = IntervalIndex | ||
|
||
def setup_method(self, method): | ||
self.index = IntervalIndex.from_arrays([0, 1], [1, 2]) | ||
self.index_with_nan = IntervalIndex.from_tuples([(0, 1), np.nan, (1, 2)]) | ||
self.indices = dict(intervalIndex=tm.makeIntervalIndex(10)) | ||
|
||
def create_index(self, closed="right"): | ||
return IntervalIndex.from_breaks(range(11), closed=closed) | ||
|
||
def test_equals(self, closed): | ||
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed) | ||
assert expected.equals(expected) | ||
assert expected.equals(expected.copy()) | ||
|
||
assert not expected.equals(expected.astype(object)) | ||
assert not expected.equals(np.array(expected)) | ||
assert not expected.equals(list(expected)) | ||
|
||
assert not expected.equals([1, 2]) | ||
assert not expected.equals(np.array([1, 2])) | ||
assert not expected.equals(date_range("20130101", periods=2)) | ||
|
||
expected_name1 = IntervalIndex.from_breaks( | ||
np.arange(5), closed=closed, name="foo" | ||
) | ||
expected_name2 = IntervalIndex.from_breaks( | ||
np.arange(5), closed=closed, name="bar" | ||
) | ||
assert expected.equals(expected_name1) | ||
assert expected_name1.equals(expected_name2) | ||
|
||
for other_closed in {"left", "right", "both", "neither"} - {closed}: | ||
expected_other_closed = IntervalIndex.from_breaks( | ||
np.arange(5), closed=other_closed | ||
) | ||
assert not expected.equals(expected_other_closed) | ||
|
||
def test_repr_max_seq_item_setting(self): | ||
# override base test: not a valid repr as we use interval notation | ||
pass | ||
|
||
def test_repr_roundtrip(self): | ||
# override base test: not a valid repr as we use interval notation | ||
pass | ||
|
||
def test_take(self, closed): | ||
index = self.create_index(closed=closed) | ||
|
||
result = index.take(range(10)) | ||
tm.assert_index_equal(result, index) | ||
|
||
result = index.take([0, 0, 1]) | ||
expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed) | ||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series]) | ||
def test_where(self, closed, klass): | ||
idx = self.create_index(closed=closed) | ||
cond = [True] * len(idx) | ||
expected = idx | ||
result = expected.where(klass(cond)) | ||
tm.assert_index_equal(result, expected) | ||
|
||
cond = [False] + [True] * len(idx[1:]) | ||
expected = IntervalIndex([np.nan] + idx[1:].tolist()) | ||
result = idx.where(klass(cond)) | ||
tm.assert_index_equal(result, expected) |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, IntervalIndex, Series, Timedelta, Timestamp | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestIntervalIndexRendering: | ||
def test_frame_repr(self): | ||
# https://github.com/pandas-dev/pandas/pull/24134/files | ||
df = DataFrame( | ||
{"A": [1, 2, 3, 4]}, index=IntervalIndex.from_breaks([0, 1, 2, 3, 4]) | ||
) | ||
result = repr(df) | ||
expected = " A\n(0, 1] 1\n(1, 2] 2\n(2, 3] 3\n(3, 4] 4" | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"constructor,expected", | ||
[ | ||
( | ||
Series, | ||
( | ||
"(0.0, 1.0] a\n" | ||
"NaN b\n" | ||
"(2.0, 3.0] c\n" | ||
"dtype: object" | ||
), | ||
), | ||
(DataFrame, (" 0\n(0.0, 1.0] a\nNaN b\n(2.0, 3.0] c")), | ||
], | ||
) | ||
def test_repr_missing(self, constructor, expected): | ||
# GH 25984 | ||
index = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)]) | ||
obj = constructor(list("abc"), index=index) | ||
result = repr(obj) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"tuples, closed, expected_data", | ||
[ | ||
([(0, 1), (1, 2), (2, 3)], "left", ["[0, 1)", "[1, 2)", "[2, 3)"]), | ||
( | ||
[(0.5, 1.0), np.nan, (2.0, 3.0)], | ||
"right", | ||
["(0.5, 1.0]", "NaN", "(2.0, 3.0]"], | ||
), | ||
( | ||
[ | ||
(Timestamp("20180101"), Timestamp("20180102")), | ||
np.nan, | ||
((Timestamp("20180102"), Timestamp("20180103"))), | ||
], | ||
"both", | ||
["[2018-01-01, 2018-01-02]", "NaN", "[2018-01-02, 2018-01-03]"], | ||
), | ||
( | ||
[ | ||
(Timedelta("0 days"), Timedelta("1 days")), | ||
(Timedelta("1 days"), Timedelta("2 days")), | ||
np.nan, | ||
], | ||
"neither", | ||
[ | ||
"(0 days 00:00:00, 1 days 00:00:00)", | ||
"(1 days 00:00:00, 2 days 00:00:00)", | ||
"NaN", | ||
], | ||
), | ||
], | ||
) | ||
def test_to_native_types(self, tuples, closed, expected_data): | ||
# GH 28210 | ||
index = IntervalIndex.from_tuples(tuples, closed=closed) | ||
result = index.to_native_types() | ||
expected = np.array(expected_data) | ||
tm.assert_numpy_array_equal(result, expected) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.