Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed May 25, 2018
1 parent 9d4691c commit f5a0c24
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 46 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ExtensionType Changes
^^^^^^^^^^^^^^^^^^^^^

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

.. _whatsnew_0240.api.other:
Expand Down
1 change: 0 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class ExtensionArray(object):
* copy
* append
* _concat_same_type
* array_type
An additional method is available to satisfy pandas' internal,
private block API.
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class ExtensionDtype(_DtypeOpsMixin):
* name
* construct_from_string
Optionally one can assign an array_type for construction with the name
of this dtype via the Registry
* array_type
The `na_value` class attribute can be used to set the default NA value
for this type. :attr:`numpy.nan` is used by default.
Expand All @@ -118,6 +124,8 @@ class ExtensionDtype(_DtypeOpsMixin):
provided for registering virtual subclasses.
"""

array_type = None

def __str__(self):
return self.name

Expand Down
56 changes: 19 additions & 37 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import re
import numpy as np
from collections import OrderedDict
from pandas import compat
from pandas.core.dtypes.generic import ABCIndexClass, ABCCategoricalIndex

Expand All @@ -18,22 +17,19 @@ class Registry(object):
These are tried in order for inference.
"""
dtypes = OrderedDict()
dtypes = []

@classmethod
def register(self, dtype, constructor=None):
def register(self, dtype):
"""
Parameters
----------
dtype : PandasExtension Dtype
dtype : ExtensionDtype
"""
if not issubclass(dtype, (PandasExtensionDtype, ExtensionDtype)):
raise ValueError("can only register pandas extension dtypes")

if constructor is None:
constructor = dtype.construct_from_string

self.dtypes[dtype] = constructor
self.dtypes.append(dtype)

def find(self, dtype):
"""
Expand All @@ -54,9 +50,9 @@ def find(self, dtype):

return None

for dtype_type, constructor in self.dtypes.items():
for dtype_type in self.dtypes:
try:
return constructor(dtype)
return dtype_type.construct_from_string(dtype)
except TypeError:
pass

Expand Down Expand Up @@ -610,29 +606,23 @@ def _parse_dtype_strict(cls, freq):
@classmethod
def construct_from_string(cls, string):
"""
attempt to construct this type from a string, raise a TypeError
if its not possible
Strict construction from a string, raise a TypeError if not
possible
"""
from pandas.tseries.offsets import DateOffset
if isinstance(string, (compat.string_types, DateOffset)):

if (isinstance(string, compat.string_types) and
(string.startswith('period[') or
string.startswith('Period[')) or
isinstance(string, DateOffset)):
# do not parse string like U as period[U]
# avoid tuple to be regarded as freq
try:
return cls(freq=string)
except ValueError:
pass
raise TypeError("could not construct PeriodDtype")

@classmethod
def construct_from_string_strict(cls, string):
"""
Strict construction from a string, raise a TypeError if not
possible
"""
if string.startswith('period[') or string.startswith('Period['):
# do not parse string like U as period[U]
return PeriodDtype.construct_from_string(string)
raise TypeError("could not construct PeriodDtype")

def __unicode__(self):
return "period[{freq}]".format(freq=self.freq.freqstr)

Expand Down Expand Up @@ -747,21 +737,13 @@ def construct_from_string(cls, string):
attempt to construct this type from a string, raise a TypeError
if its not possible
"""
if isinstance(string, compat.string_types):
if (isinstance(string, compat.string_types) and
(string.startswith('interval') or
string.startswith('Interval'))):
return cls(string)
msg = "a string needs to be passed, got type {typ}"
raise TypeError(msg.format(typ=type(string)))

@classmethod
def construct_from_string_strict(cls, string):
"""
Strict construction from a string, raise a TypeError if not
possible
"""
if string.startswith('interval') or string.startswith('Interval'):
return IntervalDtype.construct_from_string(string)
raise TypeError("cannot construct IntervalDtype")

def __unicode__(self):
if self.subtype is None:
return "interval"
Expand Down Expand Up @@ -806,6 +788,6 @@ def is_dtype(cls, dtype):

# register the dtypes in search order
registry.register(DatetimeTZDtype)
registry.register(PeriodDtype, PeriodDtype.construct_from_string_strict)
registry.register(IntervalDtype, IntervalDtype.construct_from_string_strict)
registry.register(PeriodDtype)
registry.register(IntervalDtype)
registry.register(CategoricalDtype)
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def astype(self, dtype, copy=True):
@cache_readonly
def dtype(self):
"""Return the dtype object of the underlying data"""
return IntervalDtype.construct_from_string(str(self.left.dtype))
return IntervalDtype(str(self.left.dtype))

@property
def inferred_type(self):
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/extension/base/ops.py

This file was deleted.

0 comments on commit f5a0c24

Please sign in to comment.