Skip to content

Commit a854f06

Browse files
committed
review comments
1 parent 56f0a9b commit a854f06

File tree

6 files changed

+29
-46
lines changed

6 files changed

+29
-46
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ExtensionType Changes
2727
^^^^^^^^^^^^^^^^^^^^^
2828

2929
- ``ExtensionArray`` has gained the abstract methods ``.dropna()`` and ``.append()``, and attribute ``array_type`` (:issue:`21185`)
30-
- ``ExtensionDtype`` has gained the ability to instantiate from string dtypes, e.g. ``decimal`` would instaniate a registered ``DecimalDtype`` (:issue:`21185`)
30+
- ``ExtensionDtype`` has gained the ability to instantiate from string dtypes, e.g. ``decimal`` would instantiate a registered ``DecimalDtype`` (:issue:`21185`)
3131
- The ``ExtensionArray`` constructor, ``_from_sequence`` now take the keyword arg ``copy=False`` (:issue:`21185`)
3232

3333
.. _whatsnew_0240.api.other:

pandas/core/arrays/base.py

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class ExtensionArray(object):
3838
* copy
3939
* append
4040
* _concat_same_type
41-
* array_type
4241
4342
An additional method is available to satisfy pandas' internal,
4443
private block API.

pandas/core/dtypes/base.py

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class ExtensionDtype(_DtypeOpsMixin):
109109
* name
110110
* construct_from_string
111111
112+
113+
Optionally one can assign an array_type for construction with the name
114+
of this dtype via the Registry
115+
116+
* array_type
117+
112118
The `na_value` class attribute can be used to set the default NA value
113119
for this type. :attr:`numpy.nan` is used by default.
114120
@@ -118,6 +124,8 @@ class ExtensionDtype(_DtypeOpsMixin):
118124
provided for registering virtual subclasses.
119125
"""
120126

127+
array_type = None
128+
121129
def __str__(self):
122130
return self.name
123131

pandas/core/dtypes/dtypes.py

+19-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import re
44
import numpy as np
5-
from collections import OrderedDict
65
from pandas import compat
76
from pandas.core.dtypes.generic import ABCIndexClass, ABCCategoricalIndex
87

@@ -18,22 +17,19 @@ class Registry(object):
1817
1918
These are tried in order for inference.
2019
"""
21-
dtypes = OrderedDict()
20+
dtypes = []
2221

2322
@classmethod
24-
def register(self, dtype, constructor=None):
23+
def register(self, dtype):
2524
"""
2625
Parameters
2726
----------
28-
dtype : PandasExtension Dtype
27+
dtype : ExtensionDtype
2928
"""
3029
if not issubclass(dtype, (PandasExtensionDtype, ExtensionDtype)):
3130
raise ValueError("can only register pandas extension dtypes")
3231

33-
if constructor is None:
34-
constructor = dtype.construct_from_string
35-
36-
self.dtypes[dtype] = constructor
32+
self.dtypes.append(dtype)
3733

3834
def find(self, dtype):
3935
"""
@@ -54,9 +50,9 @@ def find(self, dtype):
5450

5551
return None
5652

57-
for dtype_type, constructor in self.dtypes.items():
53+
for dtype_type in self.dtypes:
5854
try:
59-
return constructor(dtype)
55+
return dtype_type.construct_from_string(dtype)
6056
except TypeError:
6157
pass
6258

@@ -610,29 +606,23 @@ def _parse_dtype_strict(cls, freq):
610606
@classmethod
611607
def construct_from_string(cls, string):
612608
"""
613-
attempt to construct this type from a string, raise a TypeError
614-
if its not possible
609+
Strict construction from a string, raise a TypeError if not
610+
possible
615611
"""
616612
from pandas.tseries.offsets import DateOffset
617-
if isinstance(string, (compat.string_types, DateOffset)):
613+
614+
if (isinstance(string, compat.string_types) and
615+
(string.startswith('period[') or
616+
string.startswith('Period[')) or
617+
isinstance(string, DateOffset)):
618+
# do not parse string like U as period[U]
618619
# avoid tuple to be regarded as freq
619620
try:
620621
return cls(freq=string)
621622
except ValueError:
622623
pass
623624
raise TypeError("could not construct PeriodDtype")
624625

625-
@classmethod
626-
def construct_from_string_strict(cls, string):
627-
"""
628-
Strict construction from a string, raise a TypeError if not
629-
possible
630-
"""
631-
if string.startswith('period[') or string.startswith('Period['):
632-
# do not parse string like U as period[U]
633-
return PeriodDtype.construct_from_string(string)
634-
raise TypeError("could not construct PeriodDtype")
635-
636626
def __unicode__(self):
637627
return "period[{freq}]".format(freq=self.freq.freqstr)
638628

@@ -747,21 +737,13 @@ def construct_from_string(cls, string):
747737
attempt to construct this type from a string, raise a TypeError
748738
if its not possible
749739
"""
750-
if isinstance(string, compat.string_types):
740+
if (isinstance(string, compat.string_types) and
741+
(string.startswith('interval') or
742+
string.startswith('Interval'))):
751743
return cls(string)
752744
msg = "a string needs to be passed, got type {typ}"
753745
raise TypeError(msg.format(typ=type(string)))
754746

755-
@classmethod
756-
def construct_from_string_strict(cls, string):
757-
"""
758-
Strict construction from a string, raise a TypeError if not
759-
possible
760-
"""
761-
if string.startswith('interval') or string.startswith('Interval'):
762-
return IntervalDtype.construct_from_string(string)
763-
raise TypeError("cannot construct IntervalDtype")
764-
765747
def __unicode__(self):
766748
if self.subtype is None:
767749
return "interval"
@@ -806,6 +788,6 @@ def is_dtype(cls, dtype):
806788

807789
# register the dtypes in search order
808790
registry.register(DatetimeTZDtype)
809-
registry.register(PeriodDtype, PeriodDtype.construct_from_string_strict)
810-
registry.register(IntervalDtype, IntervalDtype.construct_from_string_strict)
791+
registry.register(PeriodDtype)
792+
registry.register(IntervalDtype)
811793
registry.register(CategoricalDtype)

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def astype(self, dtype, copy=True):
800800
@cache_readonly
801801
def dtype(self):
802802
"""Return the dtype object of the underlying data"""
803-
return IntervalDtype.construct_from_string(str(self.left.dtype))
803+
return IntervalDtype(str(self.left.dtype))
804804

805805
@property
806806
def inferred_type(self):

pandas/tests/extension/base/ops.py

-6
This file was deleted.

0 commit comments

Comments
 (0)