Skip to content

Commit f58d815

Browse files
authored
TST: GH38718 fix an xfail test and add new test for expected behaviour when casting interval range from float to int (#38719)
1 parent 0976c4c commit f58d815

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

pandas/tests/indexes/interval/test_astype.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -153,22 +155,25 @@ def test_subtype_integer(self, subtype):
153155
with pytest.raises(ValueError, match=msg):
154156
index.insert(0, np.nan).astype(dtype)
155157

156-
@pytest.mark.xfail(reason="GH#15832")
158+
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
159+
def test_subtype_integer_with_non_integer_borders(self, subtype):
160+
index = interval_range(0.0, 3.0, freq=0.25)
161+
dtype = IntervalDtype(subtype)
162+
result = index.astype(dtype)
163+
expected = IntervalIndex.from_arrays(
164+
index.left.astype(subtype), index.right.astype(subtype), closed=index.closed
165+
)
166+
tm.assert_index_equal(result, expected)
167+
157168
def test_subtype_integer_errors(self):
158169
# float64 -> uint64 fails with negative values
159170
index = interval_range(-10.0, 10.0)
160171
dtype = IntervalDtype("uint64")
161-
with pytest.raises(ValueError):
162-
index.astype(dtype)
163-
164-
# float64 -> integer-like fails with non-integer valued floats
165-
index = interval_range(0.0, 10.0, freq=0.25)
166-
dtype = IntervalDtype("int64")
167-
with pytest.raises(ValueError):
168-
index.astype(dtype)
169-
170-
dtype = IntervalDtype("uint64")
171-
with pytest.raises(ValueError):
172+
msg = re.escape(
173+
"Cannot convert interval[float64] to interval[uint64]; subtypes are "
174+
"incompatible"
175+
)
176+
with pytest.raises(TypeError, match=msg):
172177
index.astype(dtype)
173178

174179
@pytest.mark.parametrize("subtype", ["datetime64[ns]", "timedelta64[ns]"])

0 commit comments

Comments
 (0)