Skip to content

Commit

Permalink
remove code for unsupported dtypes and remove 'interval[]'
Browse files Browse the repository at this point in the history
  • Loading branch information
jschendel committed Jan 8, 2018
1 parent 6b00114 commit bd9805e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Conversion
- Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`)
- Bug in :class:`DatetimeIndex` where the repr was not showing high-precision time values at the end of a day (e.g., 23:59:59.999999999) (:issue:`19030`)
- Bug where dividing a scalar timedelta-like object with :class:`TimedeltaIndex` performed the reciprocal operation (:issue:`19125`)
- Bug in ``IntervalDtype`` when constructing two instances with subtype ``CategoricalDtype`` where the second instance used cached attributes from the first (:issue:`18980`)
-

Indexing
^^^^^^^^
Expand Down
17 changes: 4 additions & 13 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def __new__(cls, subtype=None):
u.subtype = None
return u
elif (isinstance(subtype, compat.string_types) and
subtype in ('interval', 'interval[]')):
subtype.lower() == 'interval'):
subtype = None
else:
if isinstance(subtype, compat.string_types):
Expand All @@ -667,27 +667,18 @@ def __new__(cls, subtype=None):
except TypeError:
raise ValueError("could not construct IntervalDtype")

if subtype is None:
u = object.__new__(cls)
u.subtype = None
return u

if is_categorical_dtype(subtype) or is_string_dtype(subtype):
# GH 19016
msg = ('category, object, and string subtypes are not supported '
'for IntervalDtype')
raise TypeError(msg)

try:
# GH 18980: need to combine since str and hash individually may not
# be unique, e.g. str(CategoricalDtype) always returns 'category',
# and hash(np.dtype('<m8')) == hash(np.dtype('<m8[ns]'))
key = ''.join([str(subtype), str(hash(subtype))])
return cls._cache[key]
return cls._cache[str(subtype)]
except KeyError:
u = object.__new__(cls)
u.subtype = subtype
cls._cache[key] = u
cls._cache[str(subtype)] = u
return u

@classmethod
Expand All @@ -712,7 +703,7 @@ def __hash__(self):

def __eq__(self, other):
if isinstance(other, compat.string_types):
return other.title() in (self.name.title(), str(self).title())
return other.lower() in (self.name.lower(), str(self).lower())
elif not isinstance(other, IntervalDtype):
return False
elif self.subtype is None or other.subtype is None:
Expand Down
21 changes: 6 additions & 15 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def test_construction(self, subtype):
assert i.subtype == np.dtype('int64')
assert is_interval_dtype(i)

@pytest.mark.parametrize('subtype', [None, 'interval', 'interval[]'])
@pytest.mark.parametrize('subtype', [None, 'interval', 'Interval'])
def test_construction_generic(self, subtype):
# generic
i = IntervalDtype(subtype)
Expand Down Expand Up @@ -535,25 +535,25 @@ def test_equality(self):
IntervalDtype('float64'))

@pytest.mark.parametrize('subtype', [
None, 'interval', 'interval[]', 'int64', 'uint64', 'float64', object,
CategoricalDtype(), 'datetime64', 'timedelta64', PeriodDtype('Q')])
None, 'interval', 'Interval', 'int64', 'uint64', 'float64',
'complex128', 'datetime64', 'timedelta64', PeriodDtype('Q')])
def test_equality_generic(self, subtype):
# GH 18980
dtype = IntervalDtype(subtype)
assert is_dtype_equal(dtype, 'interval')
assert is_dtype_equal(dtype, IntervalDtype())

@pytest.mark.parametrize('subtype', [
'int64', 'uint64', 'float64', 'complex128', np.dtype('O'),
CategoricalDtype(), 'datetime64', 'timedelta64', PeriodDtype('Q')])
'int64', 'uint64', 'float64', 'complex128', 'datetime64',
'timedelta64', PeriodDtype('Q')])
def test_name_repr(self, subtype):
# GH 18980
dtype = IntervalDtype(subtype)
expected = 'interval[{subtype}]'.format(subtype=subtype)
assert str(dtype) == expected
assert dtype.name == 'interval'

@pytest.mark.parametrize('subtype', [None, 'interval', 'interval[]'])
@pytest.mark.parametrize('subtype', [None, 'interval', 'Interval'])
def test_name_repr_generic(self, subtype):
# GH 18980
dtype = IntervalDtype(subtype)
Expand Down Expand Up @@ -600,15 +600,6 @@ def test_caching(self):
tm.round_trip_pickle(dtype)
assert len(IntervalDtype._cache) == 0

def test_caching_categoricaldtype(self):
# GH 18980
cdt1 = CategoricalDtype(list('abc'), True)
cdt2 = CategoricalDtype(list('wxyz'), False)
idt1 = IntervalDtype(cdt1)
idt2 = IntervalDtype(cdt2)
assert idt1.subtype is cdt1
assert idt2.subtype is cdt2


class TestCategoricalDtypeParametrized(object):

Expand Down

0 comments on commit bd9805e

Please sign in to comment.