|
7 | 7 | Interval, IntervalIndex, Index, isna, notna, interval_range, Timestamp,
|
8 | 8 | Timedelta, compat, date_range, timedelta_range, DateOffset)
|
9 | 9 | from pandas.compat import lzip
|
| 10 | +from pandas.core.common import _asarray_tuplesafe |
10 | 11 | from pandas.tseries.offsets import Day
|
11 | 12 | from pandas._libs.interval import IntervalTree
|
12 | 13 | from pandas.tests.indexes.common import Base
|
@@ -1072,6 +1073,45 @@ def test_is_non_overlapping_monotonic(self, closed):
|
1072 | 1073 | idx = IntervalIndex.from_breaks(range(4), closed=closed)
|
1073 | 1074 | assert idx.is_non_overlapping_monotonic is True
|
1074 | 1075 |
|
| 1076 | + @pytest.mark.parametrize('tuples', [ |
| 1077 | + lzip(range(10), range(1, 11)), |
| 1078 | + lzip(date_range('20170101', periods=10), |
| 1079 | + date_range('20170101', periods=10)), |
| 1080 | + lzip(timedelta_range('0 days', periods=10), |
| 1081 | + timedelta_range('1 day', periods=10))]) |
| 1082 | + def test_to_tuples(self, tuples): |
| 1083 | + # GH 18756 |
| 1084 | + idx = IntervalIndex.from_tuples(tuples) |
| 1085 | + result = idx.to_tuples() |
| 1086 | + expected = Index(_asarray_tuplesafe(tuples)) |
| 1087 | + tm.assert_index_equal(result, expected) |
| 1088 | + |
| 1089 | + @pytest.mark.parametrize('tuples', [ |
| 1090 | + lzip(range(10), range(1, 11)) + [np.nan], |
| 1091 | + lzip(date_range('20170101', periods=10), |
| 1092 | + date_range('20170101', periods=10)) + [np.nan], |
| 1093 | + lzip(timedelta_range('0 days', periods=10), |
| 1094 | + timedelta_range('1 day', periods=10)) + [np.nan]]) |
| 1095 | + @pytest.mark.parametrize('na_tuple', [True, False]) |
| 1096 | + def test_to_tuples_na(self, tuples, na_tuple): |
| 1097 | + # GH 18756 |
| 1098 | + idx = IntervalIndex.from_tuples(tuples) |
| 1099 | + result = idx.to_tuples(na_tuple=na_tuple) |
| 1100 | + |
| 1101 | + # check the non-NA portion |
| 1102 | + expected_notna = Index(_asarray_tuplesafe(tuples[:-1])) |
| 1103 | + result_notna = result[:-1] |
| 1104 | + tm.assert_index_equal(result_notna, expected_notna) |
| 1105 | + |
| 1106 | + # check the NA portion |
| 1107 | + result_na = result[-1] |
| 1108 | + if na_tuple: |
| 1109 | + assert isinstance(result_na, tuple) |
| 1110 | + assert len(result_na) == 2 |
| 1111 | + assert all(isna(x) for x in result_na) |
| 1112 | + else: |
| 1113 | + assert isna(result_na) |
| 1114 | + |
1075 | 1115 |
|
1076 | 1116 | class TestIntervalRange(object):
|
1077 | 1117 |
|
|
0 commit comments