|
2 | 2 |
|
3 | 3 | import re
|
4 | 4 | import numpy as np
|
| 5 | +from collections import OrderedDict |
5 | 6 | from pandas import compat
|
6 | 7 | from pandas.core.dtypes.generic import ABCIndexClass, ABCCategoricalIndex
|
7 | 8 |
|
8 | 9 | from .base import ExtensionDtype, _DtypeOpsMixin
|
9 | 10 |
|
10 | 11 |
|
| 12 | +class Registry: |
| 13 | + """ class to register our dtypes for inference |
| 14 | +
|
| 15 | + We can directly construct dtypes in pandas_dtypes if they are |
| 16 | + a type; the registry allows us to register an extension dtype |
| 17 | + to try inference from a string or a dtype class |
| 18 | +
|
| 19 | + These are tried in order for inference. |
| 20 | + """ |
| 21 | + dtypes = OrderedDict() |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def register(self, dtype, constructor=None): |
| 25 | + """ |
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + dtype : PandasExtension Dtype |
| 29 | + """ |
| 30 | + if not issubclass(dtype, PandasExtensionDtype): |
| 31 | + raise ValueError("can only register pandas extension dtypes") |
| 32 | + |
| 33 | + if constructor is None: |
| 34 | + constructor = dtype.construct_from_string |
| 35 | + |
| 36 | + self.dtypes[dtype] = constructor |
| 37 | + |
| 38 | + def find(self, dtype): |
| 39 | + """ |
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + dtype : PandasExtensionDtype or string |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + return the first matching dtype, otherwise return None |
| 47 | + """ |
| 48 | + for dtype_type, constructor in self.dtypes.items(): |
| 49 | + if isinstance(dtype, dtype_type): |
| 50 | + return dtype |
| 51 | + if isinstance(dtype, compat.string_types): |
| 52 | + try: |
| 53 | + return constructor(dtype) |
| 54 | + except TypeError: |
| 55 | + pass |
| 56 | + |
| 57 | + return None |
| 58 | + |
| 59 | + |
| 60 | +registry = Registry() |
| 61 | + |
| 62 | + |
11 | 63 | class PandasExtensionDtype(_DtypeOpsMixin):
|
12 | 64 | """
|
13 | 65 | A np.dtype duck-typed class, suitable for holding a custom dtype.
|
@@ -564,6 +616,17 @@ def construct_from_string(cls, string):
|
564 | 616 | pass
|
565 | 617 | raise TypeError("could not construct PeriodDtype")
|
566 | 618 |
|
| 619 | + @classmethod |
| 620 | + def construct_from_string_strict(cls, string): |
| 621 | + """ |
| 622 | + Strict construction from a string, raise a TypeError if not |
| 623 | + possible |
| 624 | + """ |
| 625 | + if string.startswith('period[') or string.startswith('Period['): |
| 626 | + # do not parse string like U as period[U] |
| 627 | + return PeriodDtype.construct_from_string(string) |
| 628 | + raise TypeError("could not construct PeriodDtype") |
| 629 | + |
567 | 630 | def __unicode__(self):
|
568 | 631 | return "period[{freq}]".format(freq=self.freq.freqstr)
|
569 | 632 |
|
@@ -683,6 +746,16 @@ def construct_from_string(cls, string):
|
683 | 746 | msg = "a string needs to be passed, got type {typ}"
|
684 | 747 | raise TypeError(msg.format(typ=type(string)))
|
685 | 748 |
|
| 749 | + @classmethod |
| 750 | + def construct_from_string_strict(cls, string): |
| 751 | + """ |
| 752 | + Strict construction from a string, raise a TypeError if not |
| 753 | + possible |
| 754 | + """ |
| 755 | + if string.startswith('interval') or string.startswith('Interval'): |
| 756 | + return IntervalDtype.construct_from_string(string) |
| 757 | + raise TypeError("cannot construct IntervalDtype") |
| 758 | + |
686 | 759 | def __unicode__(self):
|
687 | 760 | if self.subtype is None:
|
688 | 761 | return "interval"
|
@@ -723,3 +796,10 @@ def is_dtype(cls, dtype):
|
723 | 796 | else:
|
724 | 797 | return False
|
725 | 798 | return super(IntervalDtype, cls).is_dtype(dtype)
|
| 799 | + |
| 800 | + |
| 801 | +# register the dtypes in search order |
| 802 | +registry.register(DatetimeTZDtype) |
| 803 | +registry.register(PeriodDtype, PeriodDtype.construct_from_string_strict) |
| 804 | +registry.register(IntervalDtype, IntervalDtype.construct_from_string_strict) |
| 805 | +registry.register(CategoricalDtype) |
0 commit comments