|
| 1 | +from __future__ import division |
| 2 | + |
| 3 | +import pytest |
| 4 | +import numpy as np |
| 5 | +from pandas import ( |
| 6 | + Index, |
| 7 | + IntervalIndex, |
| 8 | + interval_range, |
| 9 | + CategoricalIndex, |
| 10 | + Timestamp, |
| 11 | + Timedelta, |
| 12 | + NaT) |
| 13 | +from pandas.core.dtypes.dtypes import CategoricalDtype, IntervalDtype |
| 14 | +import pandas.util.testing as tm |
| 15 | + |
| 16 | + |
| 17 | +class Base(object): |
| 18 | + """Tests common to IntervalIndex with any subtype""" |
| 19 | + |
| 20 | + def test_astype_idempotent(self, index): |
| 21 | + result = index.astype('interval') |
| 22 | + tm.assert_index_equal(result, index) |
| 23 | + |
| 24 | + result = index.astype(index.dtype) |
| 25 | + tm.assert_index_equal(result, index) |
| 26 | + |
| 27 | + def test_astype_object(self, index): |
| 28 | + result = index.astype(object) |
| 29 | + expected = Index(index.values, dtype='object') |
| 30 | + tm.assert_index_equal(result, expected) |
| 31 | + assert not result.equals(index) |
| 32 | + |
| 33 | + def test_astype_category(self, index): |
| 34 | + result = index.astype('category') |
| 35 | + expected = CategoricalIndex(index.values) |
| 36 | + tm.assert_index_equal(result, expected) |
| 37 | + |
| 38 | + result = index.astype(CategoricalDtype()) |
| 39 | + tm.assert_index_equal(result, expected) |
| 40 | + |
| 41 | + # non-default params |
| 42 | + categories = index.dropna().unique().values[:-1] |
| 43 | + dtype = CategoricalDtype(categories=categories, ordered=True) |
| 44 | + result = index.astype(dtype) |
| 45 | + expected = CategoricalIndex( |
| 46 | + index.values, categories=categories, ordered=True) |
| 47 | + tm.assert_index_equal(result, expected) |
| 48 | + |
| 49 | + @pytest.mark.parametrize('dtype', [ |
| 50 | + 'int64', 'uint64', 'float64', 'complex128', 'period[M]', |
| 51 | + 'timedelta64', 'timedelta64[ns]', 'datetime64', 'datetime64[ns]', |
| 52 | + 'datetime64[ns, US/Eastern]']) |
| 53 | + def test_astype_cannot_cast(self, index, dtype): |
| 54 | + msg = 'Cannot cast IntervalIndex to dtype' |
| 55 | + with tm.assert_raises_regex(TypeError, msg): |
| 56 | + index.astype(dtype) |
| 57 | + |
| 58 | + def test_astype_invalid_dtype(self, index): |
| 59 | + msg = 'data type "fake_dtype" not understood' |
| 60 | + with tm.assert_raises_regex(TypeError, msg): |
| 61 | + index.astype('fake_dtype') |
| 62 | + |
| 63 | + |
| 64 | +class TestIntSubtype(Base): |
| 65 | + """Tests specific to IntervalIndex with integer-like subtype""" |
| 66 | + |
| 67 | + indexes = [ |
| 68 | + IntervalIndex.from_breaks(np.arange(-10, 11, dtype='int64')), |
| 69 | + IntervalIndex.from_breaks( |
| 70 | + np.arange(100, dtype='uint64'), closed='left'), |
| 71 | + ] |
| 72 | + |
| 73 | + @pytest.fixture(params=indexes) |
| 74 | + def index(self, request): |
| 75 | + return request.param |
| 76 | + |
| 77 | + @pytest.mark.parametrize('subtype', [ |
| 78 | + 'float64', 'datetime64[ns]', 'timedelta64[ns]']) |
| 79 | + def test_subtype_conversion(self, index, subtype): |
| 80 | + dtype = IntervalDtype(subtype) |
| 81 | + result = index.astype(dtype) |
| 82 | + expected = IntervalIndex.from_arrays(index.left.astype(subtype), |
| 83 | + index.right.astype(subtype), |
| 84 | + closed=index.closed) |
| 85 | + tm.assert_index_equal(result, expected) |
| 86 | + |
| 87 | + @pytest.mark.parametrize('subtype_start, subtype_end', [ |
| 88 | + ('int64', 'uint64'), ('uint64', 'int64')]) |
| 89 | + def test_subtype_integer(self, subtype_start, subtype_end): |
| 90 | + index = IntervalIndex.from_breaks(np.arange(100, dtype=subtype_start)) |
| 91 | + dtype = IntervalDtype(subtype_end) |
| 92 | + result = index.astype(dtype) |
| 93 | + expected = IntervalIndex.from_arrays(index.left.astype(subtype_end), |
| 94 | + index.right.astype(subtype_end), |
| 95 | + closed=index.closed) |
| 96 | + tm.assert_index_equal(result, expected) |
| 97 | + |
| 98 | + @pytest.mark.xfail(reason='GH 15832') |
| 99 | + def test_subtype_integer_errors(self): |
| 100 | + # int64 -> uint64 fails with negative values |
| 101 | + index = interval_range(-10, 10) |
| 102 | + dtype = IntervalDtype('uint64') |
| 103 | + with pytest.raises(ValueError): |
| 104 | + index.astype(dtype) |
| 105 | + |
| 106 | + |
| 107 | +class TestFloatSubtype(Base): |
| 108 | + """Tests specific to IntervalIndex with float subtype""" |
| 109 | + |
| 110 | + indexes = [ |
| 111 | + interval_range(-10.0, 10.0, closed='neither'), |
| 112 | + IntervalIndex.from_arrays([-1.5, np.nan, 0., 0., 1.5], |
| 113 | + [-0.5, np.nan, 1., 1., 3.], |
| 114 | + closed='both'), |
| 115 | + ] |
| 116 | + |
| 117 | + @pytest.fixture(params=indexes) |
| 118 | + def index(self, request): |
| 119 | + return request.param |
| 120 | + |
| 121 | + @pytest.mark.parametrize('subtype', ['int64', 'uint64']) |
| 122 | + def test_subtype_integer(self, subtype): |
| 123 | + index = interval_range(0.0, 10.0) |
| 124 | + dtype = IntervalDtype(subtype) |
| 125 | + result = index.astype(dtype) |
| 126 | + expected = IntervalIndex.from_arrays(index.left.astype(subtype), |
| 127 | + index.right.astype(subtype), |
| 128 | + closed=index.closed) |
| 129 | + tm.assert_index_equal(result, expected) |
| 130 | + |
| 131 | + # raises with NA |
| 132 | + msg = 'Cannot convert NA to integer' |
| 133 | + with tm.assert_raises_regex(ValueError, msg): |
| 134 | + index.insert(0, np.nan).astype(dtype) |
| 135 | + |
| 136 | + @pytest.mark.xfail(reason='GH 15832') |
| 137 | + def test_subtype_integer_errors(self): |
| 138 | + # float64 -> uint64 fails with negative values |
| 139 | + index = interval_range(-10.0, 10.0) |
| 140 | + dtype = IntervalDtype('uint64') |
| 141 | + with pytest.raises(ValueError): |
| 142 | + index.astype(dtype) |
| 143 | + |
| 144 | + # float64 -> integer-like fails with non-integer valued floats |
| 145 | + index = interval_range(0.0, 10.0, freq=0.25) |
| 146 | + dtype = IntervalDtype('int64') |
| 147 | + with pytest.raises(ValueError): |
| 148 | + index.astype(dtype) |
| 149 | + |
| 150 | + dtype = IntervalDtype('uint64') |
| 151 | + with pytest.raises(ValueError): |
| 152 | + index.astype(dtype) |
| 153 | + |
| 154 | + @pytest.mark.parametrize('subtype', ['datetime64[ns]', 'timedelta64[ns]']) |
| 155 | + def test_subtype_datetimelike(self, index, subtype): |
| 156 | + dtype = IntervalDtype(subtype) |
| 157 | + msg = 'Cannot convert .* to .*; subtypes are incompatible' |
| 158 | + with tm.assert_raises_regex(TypeError, msg): |
| 159 | + index.astype(dtype) |
| 160 | + |
| 161 | + |
| 162 | +class TestDatetimelikeSubtype(Base): |
| 163 | + """Tests specific to IntervalIndex with datetime-like subtype""" |
| 164 | + |
| 165 | + indexes = [ |
| 166 | + interval_range(Timestamp('2018-01-01'), periods=10, closed='neither'), |
| 167 | + interval_range(Timestamp('2018-01-01'), periods=10).insert(2, NaT), |
| 168 | + interval_range(Timestamp('2018-01-01', tz='US/Eastern'), periods=10), |
| 169 | + interval_range(Timedelta('0 days'), periods=10, closed='both'), |
| 170 | + interval_range(Timedelta('0 days'), periods=10).insert(2, NaT), |
| 171 | + ] |
| 172 | + |
| 173 | + @pytest.fixture(params=indexes) |
| 174 | + def index(self, request): |
| 175 | + return request.param |
| 176 | + |
| 177 | + @pytest.mark.parametrize('subtype', ['int64', 'uint64']) |
| 178 | + def test_subtype_integer(self, index, subtype): |
| 179 | + dtype = IntervalDtype(subtype) |
| 180 | + result = index.astype(dtype) |
| 181 | + expected = IntervalIndex.from_arrays(index.left.astype(subtype), |
| 182 | + index.right.astype(subtype), |
| 183 | + closed=index.closed) |
| 184 | + tm.assert_index_equal(result, expected) |
| 185 | + |
| 186 | + def test_subtype_float(self, index): |
| 187 | + dtype = IntervalDtype('float64') |
| 188 | + msg = 'Cannot convert .* to .*; subtypes are incompatible' |
| 189 | + with tm.assert_raises_regex(TypeError, msg): |
| 190 | + index.astype(dtype) |
| 191 | + |
| 192 | + def test_subtype_datetimelike(self): |
| 193 | + # datetime -> timedelta raises |
| 194 | + dtype = IntervalDtype('timedelta64[ns]') |
| 195 | + msg = 'Cannot convert .* to .*; subtypes are incompatible' |
| 196 | + |
| 197 | + index = interval_range(Timestamp('2018-01-01'), periods=10) |
| 198 | + with tm.assert_raises_regex(TypeError, msg): |
| 199 | + index.astype(dtype) |
| 200 | + |
| 201 | + index = interval_range(Timestamp('2018-01-01', tz='CET'), periods=10) |
| 202 | + with tm.assert_raises_regex(TypeError, msg): |
| 203 | + index.astype(dtype) |
| 204 | + |
| 205 | + # timedelta -> datetime raises |
| 206 | + dtype = IntervalDtype('datetime64[ns]') |
| 207 | + index = interval_range(Timedelta('0 days'), periods=10) |
| 208 | + with tm.assert_raises_regex(TypeError, msg): |
| 209 | + index.astype(dtype) |
0 commit comments