|
5 | 5 | from datetime import datetime, timedelta |
6 | 6 |
|
7 | 7 | import pandas.util.testing as tm |
| 8 | +from pandas.core.dtypes.common import is_unsigned_integer_dtype |
8 | 9 | from pandas.core.indexes.api import Index, MultiIndex |
9 | 10 | from pandas.tests.indexes.common import Base |
10 | 11 |
|
@@ -201,25 +202,20 @@ def __array__(self, dtype=None): |
201 | 202 | result = pd.Index(ArrayLike(array)) |
202 | 203 | tm.assert_index_equal(result, expected) |
203 | 204 |
|
204 | | - def test_constructor_int_dtype_float(self): |
| 205 | + @pytest.mark.parametrize('dtype', [ |
| 206 | + int, 'int64', 'int32', 'int16', 'int8', 'uint64', 'uint32', |
| 207 | + 'uint16', 'uint8']) |
| 208 | + def test_constructor_int_dtype_float(self, dtype): |
205 | 209 | # GH 18400 |
206 | | - data = [0., 1., 2., 3.] |
207 | | - |
208 | | - expected = Int64Index([0, 1, 2, 3]) |
209 | | - result = Index(data, dtype='int64') |
210 | | - tm.assert_index_equal(result, expected) |
| 210 | + if is_unsigned_integer_dtype(dtype): |
| 211 | + index_type = UInt64Index |
| 212 | + else: |
| 213 | + index_type = Int64Index |
211 | 214 |
|
212 | | - expected = UInt64Index([0, 1, 2, 3]) |
213 | | - result = Index(data, dtype='uint64') |
| 215 | + expected = index_type([0, 1, 2, 3]) |
| 216 | + result = Index([0., 1., 2., 3.], dtype=dtype) |
214 | 217 | tm.assert_index_equal(result, expected) |
215 | 218 |
|
216 | | - # fall back to Float64Index |
217 | | - data = [0.0, 1.1, 2.2, 3.3] |
218 | | - expected = Float64Index(data) |
219 | | - for dtype in ('int64', 'uint64'): |
220 | | - result = Index(data, dtype=dtype) |
221 | | - tm.assert_index_equal(result, expected) |
222 | | - |
223 | 219 | def test_constructor_int_dtype_nan(self): |
224 | 220 | # see gh-15187 |
225 | 221 | data = [np.nan] |
|
0 commit comments