From 6b001146cf093ac6459fb34f42a7a1cc668402be Mon Sep 17 00:00:00 2001 From: jschendel Date: Fri, 29 Dec 2017 13:27:57 -0700 Subject: [PATCH 1/2] Fix IntervalDtype Bugs and Inconsistencies --- doc/source/whatsnew/v0.23.0.txt | 3 +- pandas/core/dtypes/dtypes.py | 37 ++++++----- pandas/tests/dtypes/test_dtypes.py | 103 +++++++++++++++++++---------- 3 files changed, 90 insertions(+), 53 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index d7a3f0d077302..65e0062fea0cb 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -270,6 +270,7 @@ Other API Changes - Subtraction of :class:`Series` with timezone-aware ``dtype='datetime64[ns]'`` with mis-matched timezones will raise ``TypeError`` instead of ``ValueError`` (issue:`18817`) - :class:`IntervalIndex` and ``IntervalDtype`` no longer support categorical, object, and string subtypes (:issue:`19016`) - The default ``Timedelta`` constructor now accepts an ``ISO 8601 Duration`` string as an argument (:issue:`19040`) +- ``IntervalDtype`` now returns ``True`` when compared against ``'interval'`` regardless of subtype, and ``IntervalDtype.name`` now returns ``'interval'`` regardless of subtype (:issue:`18980`) .. _whatsnew_0230.deprecations: @@ -377,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 ^^^^^^^^ diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 08773354d44d8..fce371bec363d 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -626,6 +626,7 @@ class IntervalDtype(ExtensionDtype): THIS IS NOT A REAL NUMPY DTYPE """ + name = 'interval' type = IntervalDtypeType kind = None str = '|O08' @@ -653,8 +654,8 @@ def __new__(cls, subtype=None): u.subtype = None return u elif (isinstance(subtype, compat.string_types) and - subtype == 'interval'): - subtype = '' + subtype in ('interval', 'interval[]')): + subtype = None else: if isinstance(subtype, compat.string_types): m = cls._match.search(subtype) @@ -678,11 +679,15 @@ def __new__(cls, subtype=None): raise TypeError(msg) try: - return cls._cache[str(subtype)] + # 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(' Date: Sat, 6 Jan 2018 20:47:18 -0700 Subject: [PATCH 2/2] remove code for unsupported dtypes and remove 'interval[]' --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/core/dtypes/dtypes.py | 17 ++++------------- pandas/tests/dtypes/test_dtypes.py | 21 ++++++--------------- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 65e0062fea0cb..783de6569a542 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -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 ^^^^^^^^ diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index fce371bec363d..2ec35889d6a7a 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -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): @@ -667,11 +667,6 @@ 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 ' @@ -679,15 +674,11 @@ def __new__(cls, subtype=None): 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('