Skip to content

Commit 8753f17

Browse files
committed
interval dtypes
1 parent 7256c13 commit 8753f17

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ExtensionType Changes
3636
Other API Changes
3737
^^^^^^^^^^^^^^^^^
3838

39-
-
39+
- Invalid consruction of ``IntervalDtype`` will now always raise a ``TypeError`` rather than a ``ValueError`` if the subdtype is invalid (:issue:`21185`)
4040
-
4141
-
4242

pandas/core/dtypes/dtypes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def __new__(cls, subtype=None):
730730
try:
731731
subtype = pandas_dtype(subtype)
732732
except TypeError:
733-
raise ValueError("could not construct IntervalDtype")
733+
raise TypeError("could not construct IntervalDtype")
734734

735735
if is_categorical_dtype(subtype) or is_string_dtype(subtype):
736736
# GH 19016
@@ -756,6 +756,7 @@ def construct_from_string(cls, string):
756756
(string.startswith('interval') or
757757
string.startswith('Interval'))):
758758
return cls(string)
759+
759760
msg = "a string needs to be passed, got type {typ}"
760761
raise TypeError(msg.format(typ=type(string)))
761762

pandas/tests/dtypes/test_dtypes.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pandas import (
1010
Series, Categorical, CategoricalIndex, IntervalIndex, date_range)
1111

12-
from pandas.compat import string_types
1312
from pandas.core.dtypes.dtypes import (
1413
DatetimeTZDtype, PeriodDtype,
1514
IntervalDtype, CategoricalDtype, registry)
@@ -448,7 +447,7 @@ def test_construction_not_supported(self, subtype):
448447

449448
def test_construction_errors(self):
450449
msg = 'could not construct IntervalDtype'
451-
with tm.assert_raises_regex(ValueError, msg):
450+
with tm.assert_raises_regex(TypeError, msg):
452451
IntervalDtype('xx')
453452

454453
def test_construction_from_string(self):
@@ -458,14 +457,21 @@ def test_construction_from_string(self):
458457
assert is_dtype_equal(self.dtype, result)
459458

460459
@pytest.mark.parametrize('string', [
461-
'foo', 'interval[foo]', 'foo[int64]', 0, 3.14, ('a', 'b'), None])
460+
'foo', 'foo[int64]', 0, 3.14, ('a', 'b'), None])
462461
def test_construction_from_string_errors(self, string):
463-
if isinstance(string, string_types):
464-
error, msg = ValueError, 'could not construct IntervalDtype'
465-
else:
466-
error, msg = TypeError, 'a string needs to be passed, got type'
462+
# these are invalid entirely
463+
msg = 'a string needs to be passed, got type'
464+
465+
with tm.assert_raises_regex(TypeError, msg):
466+
IntervalDtype.construct_from_string(string)
467467

468-
with tm.assert_raises_regex(error, msg):
468+
@pytest.mark.parametrize('string', [
469+
'interval[foo]'])
470+
def test_construction_from_string_error_subtype(self, string):
471+
# this is an invalid subtype
472+
msg = 'could not construct IntervalDtype'
473+
474+
with tm.assert_raises_regex(TypeError, msg):
469475
IntervalDtype.construct_from_string(string)
470476

471477
def test_subclass(self):

0 commit comments

Comments
 (0)