|
1 |
| -from distutils.version import LooseVersion |
2 | 1 | import json
|
3 | 2 |
|
4 | 3 | import numpy as np
|
5 | 4 | import pyarrow
|
6 | 5 |
|
7 | 6 | from pandas.core.arrays.interval import VALID_CLOSED
|
8 | 7 |
|
9 |
| -_pyarrow_version_ge_015 = LooseVersion(pyarrow.__version__) >= LooseVersion("0.15") |
10 |
| - |
11 | 8 |
|
12 | 9 | def pyarrow_array_to_numpy_and_mask(arr, dtype):
|
13 | 10 | """
|
@@ -48,97 +45,97 @@ def pyarrow_array_to_numpy_and_mask(arr, dtype):
|
48 | 45 | return data, mask
|
49 | 46 |
|
50 | 47 |
|
51 |
| -if _pyarrow_version_ge_015: |
52 |
| - # the pyarrow extension types are only available for pyarrow 0.15+ |
53 |
| - |
54 |
| - class ArrowPeriodType(pyarrow.ExtensionType): |
55 |
| - def __init__(self, freq): |
56 |
| - # attributes need to be set first before calling |
57 |
| - # super init (as that calls serialize) |
58 |
| - self._freq = freq |
59 |
| - pyarrow.ExtensionType.__init__(self, pyarrow.int64(), "pandas.period") |
60 |
| - |
61 |
| - @property |
62 |
| - def freq(self): |
63 |
| - return self._freq |
64 |
| - |
65 |
| - def __arrow_ext_serialize__(self): |
66 |
| - metadata = {"freq": self.freq} |
67 |
| - return json.dumps(metadata).encode() |
68 |
| - |
69 |
| - @classmethod |
70 |
| - def __arrow_ext_deserialize__(cls, storage_type, serialized): |
71 |
| - metadata = json.loads(serialized.decode()) |
72 |
| - return ArrowPeriodType(metadata["freq"]) |
73 |
| - |
74 |
| - def __eq__(self, other): |
75 |
| - if isinstance(other, pyarrow.BaseExtensionType): |
76 |
| - return type(self) == type(other) and self.freq == other.freq |
77 |
| - else: |
78 |
| - return NotImplemented |
79 |
| - |
80 |
| - def __hash__(self): |
81 |
| - return hash((str(self), self.freq)) |
82 |
| - |
83 |
| - def to_pandas_dtype(self): |
84 |
| - import pandas as pd |
85 |
| - |
86 |
| - return pd.PeriodDtype(freq=self.freq) |
87 |
| - |
88 |
| - # register the type with a dummy instance |
89 |
| - _period_type = ArrowPeriodType("D") |
90 |
| - pyarrow.register_extension_type(_period_type) |
91 |
| - |
92 |
| - class ArrowIntervalType(pyarrow.ExtensionType): |
93 |
| - def __init__(self, subtype, closed): |
94 |
| - # attributes need to be set first before calling |
95 |
| - # super init (as that calls serialize) |
96 |
| - assert closed in VALID_CLOSED |
97 |
| - self._closed = closed |
98 |
| - if not isinstance(subtype, pyarrow.DataType): |
99 |
| - subtype = pyarrow.type_for_alias(str(subtype)) |
100 |
| - self._subtype = subtype |
101 |
| - |
102 |
| - storage_type = pyarrow.struct([("left", subtype), ("right", subtype)]) |
103 |
| - pyarrow.ExtensionType.__init__(self, storage_type, "pandas.interval") |
104 |
| - |
105 |
| - @property |
106 |
| - def subtype(self): |
107 |
| - return self._subtype |
108 |
| - |
109 |
| - @property |
110 |
| - def closed(self): |
111 |
| - return self._closed |
112 |
| - |
113 |
| - def __arrow_ext_serialize__(self): |
114 |
| - metadata = {"subtype": str(self.subtype), "closed": self.closed} |
115 |
| - return json.dumps(metadata).encode() |
116 |
| - |
117 |
| - @classmethod |
118 |
| - def __arrow_ext_deserialize__(cls, storage_type, serialized): |
119 |
| - metadata = json.loads(serialized.decode()) |
120 |
| - subtype = pyarrow.type_for_alias(metadata["subtype"]) |
121 |
| - closed = metadata["closed"] |
122 |
| - return ArrowIntervalType(subtype, closed) |
123 |
| - |
124 |
| - def __eq__(self, other): |
125 |
| - if isinstance(other, pyarrow.BaseExtensionType): |
126 |
| - return ( |
127 |
| - type(self) == type(other) |
128 |
| - and self.subtype == other.subtype |
129 |
| - and self.closed == other.closed |
130 |
| - ) |
131 |
| - else: |
132 |
| - return NotImplemented |
133 |
| - |
134 |
| - def __hash__(self): |
135 |
| - return hash((str(self), str(self.subtype), self.closed)) |
136 |
| - |
137 |
| - def to_pandas_dtype(self): |
138 |
| - import pandas as pd |
139 |
| - |
140 |
| - return pd.IntervalDtype(self.subtype.to_pandas_dtype(), self.closed) |
141 |
| - |
142 |
| - # register the type with a dummy instance |
143 |
| - _interval_type = ArrowIntervalType(pyarrow.int64(), "left") |
144 |
| - pyarrow.register_extension_type(_interval_type) |
| 48 | +class ArrowPeriodType(pyarrow.ExtensionType): |
| 49 | + def __init__(self, freq): |
| 50 | + # attributes need to be set first before calling |
| 51 | + # super init (as that calls serialize) |
| 52 | + self._freq = freq |
| 53 | + pyarrow.ExtensionType.__init__(self, pyarrow.int64(), "pandas.period") |
| 54 | + |
| 55 | + @property |
| 56 | + def freq(self): |
| 57 | + return self._freq |
| 58 | + |
| 59 | + def __arrow_ext_serialize__(self): |
| 60 | + metadata = {"freq": self.freq} |
| 61 | + return json.dumps(metadata).encode() |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def __arrow_ext_deserialize__(cls, storage_type, serialized): |
| 65 | + metadata = json.loads(serialized.decode()) |
| 66 | + return ArrowPeriodType(metadata["freq"]) |
| 67 | + |
| 68 | + def __eq__(self, other): |
| 69 | + if isinstance(other, pyarrow.BaseExtensionType): |
| 70 | + return type(self) == type(other) and self.freq == other.freq |
| 71 | + else: |
| 72 | + return NotImplemented |
| 73 | + |
| 74 | + def __hash__(self): |
| 75 | + return hash((str(self), self.freq)) |
| 76 | + |
| 77 | + def to_pandas_dtype(self): |
| 78 | + import pandas as pd |
| 79 | + |
| 80 | + return pd.PeriodDtype(freq=self.freq) |
| 81 | + |
| 82 | + |
| 83 | +# register the type with a dummy instance |
| 84 | +_period_type = ArrowPeriodType("D") |
| 85 | +pyarrow.register_extension_type(_period_type) |
| 86 | + |
| 87 | + |
| 88 | +class ArrowIntervalType(pyarrow.ExtensionType): |
| 89 | + def __init__(self, subtype, closed): |
| 90 | + # attributes need to be set first before calling |
| 91 | + # super init (as that calls serialize) |
| 92 | + assert closed in VALID_CLOSED |
| 93 | + self._closed = closed |
| 94 | + if not isinstance(subtype, pyarrow.DataType): |
| 95 | + subtype = pyarrow.type_for_alias(str(subtype)) |
| 96 | + self._subtype = subtype |
| 97 | + |
| 98 | + storage_type = pyarrow.struct([("left", subtype), ("right", subtype)]) |
| 99 | + pyarrow.ExtensionType.__init__(self, storage_type, "pandas.interval") |
| 100 | + |
| 101 | + @property |
| 102 | + def subtype(self): |
| 103 | + return self._subtype |
| 104 | + |
| 105 | + @property |
| 106 | + def closed(self): |
| 107 | + return self._closed |
| 108 | + |
| 109 | + def __arrow_ext_serialize__(self): |
| 110 | + metadata = {"subtype": str(self.subtype), "closed": self.closed} |
| 111 | + return json.dumps(metadata).encode() |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def __arrow_ext_deserialize__(cls, storage_type, serialized): |
| 115 | + metadata = json.loads(serialized.decode()) |
| 116 | + subtype = pyarrow.type_for_alias(metadata["subtype"]) |
| 117 | + closed = metadata["closed"] |
| 118 | + return ArrowIntervalType(subtype, closed) |
| 119 | + |
| 120 | + def __eq__(self, other): |
| 121 | + if isinstance(other, pyarrow.BaseExtensionType): |
| 122 | + return ( |
| 123 | + type(self) == type(other) |
| 124 | + and self.subtype == other.subtype |
| 125 | + and self.closed == other.closed |
| 126 | + ) |
| 127 | + else: |
| 128 | + return NotImplemented |
| 129 | + |
| 130 | + def __hash__(self): |
| 131 | + return hash((str(self), str(self.subtype), self.closed)) |
| 132 | + |
| 133 | + def to_pandas_dtype(self): |
| 134 | + import pandas as pd |
| 135 | + |
| 136 | + return pd.IntervalDtype(self.subtype.to_pandas_dtype(), self.closed) |
| 137 | + |
| 138 | + |
| 139 | +# register the type with a dummy instance |
| 140 | +_interval_type = ArrowIntervalType(pyarrow.int64(), "left") |
| 141 | +pyarrow.register_extension_type(_interval_type) |
0 commit comments