|
4 | 4 | import numpy as np
|
5 | 5 | from pandas import (
|
6 | 6 | Interval, IntervalIndex, Index, isna, notna, interval_range, Timestamp,
|
7 |
| - Timedelta, date_range, timedelta_range) |
| 7 | + Timedelta, date_range, timedelta_range, Categorical) |
8 | 8 | from pandas.compat import lzip
|
9 | 9 | from pandas.core.common import _asarray_tuplesafe
|
10 | 10 | from pandas.tests.indexes.common import Base
|
@@ -42,7 +42,6 @@ def create_index_with_nan(self, closed='right'):
|
42 | 42 |
|
43 | 43 | @pytest.mark.parametrize('data', [
|
44 | 44 | Index([0, 1, 2, 3, 4]),
|
45 |
| - Index(list('abcde')), |
46 | 45 | date_range('2017-01-01', periods=5),
|
47 | 46 | date_range('2017-01-01', periods=5, tz='US/Eastern'),
|
48 | 47 | timedelta_range('1 day', periods=5)])
|
@@ -138,10 +137,10 @@ def test_constructors_nan(self, closed, data):
|
138 | 137 | [],
|
139 | 138 | np.array([], dtype='int64'),
|
140 | 139 | np.array([], dtype='float64'),
|
141 |
| - np.array([], dtype=object)]) |
| 140 | + np.array([], dtype='datetime64[ns]')]) |
142 | 141 | def test_constructors_empty(self, data, closed):
|
143 | 142 | # GH 18421
|
144 |
| - expected_dtype = data.dtype if isinstance(data, np.ndarray) else object |
| 143 | + expected_dtype = getattr(data, 'dtype', np.intp) |
145 | 144 | expected_values = np.array([], dtype=object)
|
146 | 145 | expected_index = IntervalIndex(data, closed=closed)
|
147 | 146 |
|
@@ -223,6 +222,48 @@ def test_constructors_errors(self):
|
223 | 222 | with tm.assert_raises_regex(ValueError, msg):
|
224 | 223 | IntervalIndex.from_arrays(range(10, -1, -1), range(9, -2, -1))
|
225 | 224 |
|
| 225 | + # GH 19016: categorical data |
| 226 | + data = Categorical(list('01234abcde'), ordered=True) |
| 227 | + msg = ('category, object, and string subtypes are not supported ' |
| 228 | + 'for IntervalIndex') |
| 229 | + |
| 230 | + with tm.assert_raises_regex(TypeError, msg): |
| 231 | + IntervalIndex.from_breaks(data) |
| 232 | + |
| 233 | + with tm.assert_raises_regex(TypeError, msg): |
| 234 | + IntervalIndex.from_arrays(data[:-1], data[1:]) |
| 235 | + |
| 236 | + @pytest.mark.parametrize('data', [ |
| 237 | + tuple('0123456789'), |
| 238 | + list('abcdefghij'), |
| 239 | + np.array(list('abcdefghij'), dtype=object), |
| 240 | + np.array(list('abcdefghij'), dtype='<U1')]) |
| 241 | + def test_constructors_errors_string(self, data): |
| 242 | + # GH 19016 |
| 243 | + left, right = data[:-1], data[1:] |
| 244 | + tuples = lzip(left, right) |
| 245 | + ivs = [Interval(l, r) for l, r in tuples] or data |
| 246 | + msg = ('category, object, and string subtypes are not supported ' |
| 247 | + 'for IntervalIndex') |
| 248 | + |
| 249 | + with tm.assert_raises_regex(TypeError, msg): |
| 250 | + IntervalIndex(ivs) |
| 251 | + |
| 252 | + with tm.assert_raises_regex(TypeError, msg): |
| 253 | + Index(ivs) |
| 254 | + |
| 255 | + with tm.assert_raises_regex(TypeError, msg): |
| 256 | + IntervalIndex.from_intervals(ivs) |
| 257 | + |
| 258 | + with tm.assert_raises_regex(TypeError, msg): |
| 259 | + IntervalIndex.from_breaks(data) |
| 260 | + |
| 261 | + with tm.assert_raises_regex(TypeError, msg): |
| 262 | + IntervalIndex.from_arrays(left, right) |
| 263 | + |
| 264 | + with tm.assert_raises_regex(TypeError, msg): |
| 265 | + IntervalIndex.from_tuples(tuples) |
| 266 | + |
226 | 267 | @pytest.mark.parametrize('tz_left, tz_right', [
|
227 | 268 | (None, 'UTC'), ('UTC', None), ('UTC', 'US/Eastern')])
|
228 | 269 | def test_constructors_errors_tz(self, tz_left, tz_right):
|
@@ -298,18 +339,6 @@ def test_length(self, closed, breaks):
|
298 | 339 | expected = Index(iv.length if notna(iv) else iv for iv in index)
|
299 | 340 | tm.assert_index_equal(result, expected)
|
300 | 341 |
|
301 |
| - @pytest.mark.parametrize('breaks', [ |
302 |
| - list('abcdefgh'), |
303 |
| - lzip(range(10), range(1, 11)), |
304 |
| - [['A', 'B'], ['a', 'b'], ['c', 'd'], ['e', 'f']], |
305 |
| - [Interval(0, 1), Interval(1, 2), Interval(3, 4), Interval(4, 5)]]) |
306 |
| - def test_length_errors(self, closed, breaks): |
307 |
| - # GH 18789 |
308 |
| - index = IntervalIndex.from_breaks(breaks) |
309 |
| - msg = 'IntervalIndex contains Intervals without defined length' |
310 |
| - with tm.assert_raises_regex(TypeError, msg): |
311 |
| - index.length |
312 |
| - |
313 | 342 | def test_with_nans(self, closed):
|
314 | 343 | index = self.create_index(closed=closed)
|
315 | 344 | assert not index.hasnans
|
@@ -428,9 +457,7 @@ def test_delete(self, closed):
|
428 | 457 | interval_range(0, periods=10, closed='neither'),
|
429 | 458 | interval_range(1.7, periods=8, freq=2.5, closed='both'),
|
430 | 459 | interval_range(Timestamp('20170101'), periods=12, closed='left'),
|
431 |
| - interval_range(Timedelta('1 day'), periods=6, closed='right'), |
432 |
| - IntervalIndex.from_tuples([('a', 'd'), ('e', 'j'), ('w', 'z')]), |
433 |
| - IntervalIndex.from_tuples([(1, 2), ('a', 'z'), (3.14, 6.28)])]) |
| 460 | + interval_range(Timedelta('1 day'), periods=6, closed='right')]) |
434 | 461 | def test_insert(self, data):
|
435 | 462 | item = data[0]
|
436 | 463 | idx_item = IntervalIndex([item])
|
@@ -504,15 +531,6 @@ def test_unique(self, closed):
|
504 | 531 | [(0, 1), (0, 1), (2, 3)], closed=closed)
|
505 | 532 | assert not idx.is_unique
|
506 | 533 |
|
507 |
| - # unique mixed |
508 |
| - idx = IntervalIndex.from_tuples([(0, 1), ('a', 'b')], closed=closed) |
509 |
| - assert idx.is_unique |
510 |
| - |
511 |
| - # duplicate mixed |
512 |
| - idx = IntervalIndex.from_tuples( |
513 |
| - [(0, 1), ('a', 'b'), (0, 1)], closed=closed) |
514 |
| - assert not idx.is_unique |
515 |
| - |
516 | 534 | # empty
|
517 | 535 | idx = IntervalIndex([], closed=closed)
|
518 | 536 | assert idx.is_unique
|
|
0 commit comments