|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import inspect
|
4 |
| -import json |
5 | 4 | import warnings
|
6 | 5 |
|
7 | 6 | import numpy as np
|
8 | 7 | import pyarrow
|
9 | 8 |
|
10 |
| -from pandas._typing import IntervalInclusiveType |
11 | 9 | from pandas.errors import PerformanceWarning
|
12 |
| -from pandas.util._decorators import deprecate_kwarg |
13 | 10 | from pandas.util._exceptions import find_stack_level
|
14 | 11 |
|
15 |
| -from pandas.core.arrays.interval import VALID_INCLUSIVE |
16 |
| - |
17 | 12 |
|
18 | 13 | def fallback_performancewarning(version: str | None = None) -> None:
|
19 | 14 | """
|
@@ -67,109 +62,3 @@ def pyarrow_array_to_numpy_and_mask(
|
67 | 62 | else:
|
68 | 63 | mask = np.ones(len(arr), dtype=bool)
|
69 | 64 | return data, mask
|
70 |
| - |
71 |
| - |
72 |
| -class ArrowPeriodType(pyarrow.ExtensionType): |
73 |
| - def __init__(self, freq) -> None: |
74 |
| - # attributes need to be set first before calling |
75 |
| - # super init (as that calls serialize) |
76 |
| - self._freq = freq |
77 |
| - pyarrow.ExtensionType.__init__(self, pyarrow.int64(), "pandas.period") |
78 |
| - |
79 |
| - @property |
80 |
| - def freq(self): |
81 |
| - return self._freq |
82 |
| - |
83 |
| - def __arrow_ext_serialize__(self) -> bytes: |
84 |
| - metadata = {"freq": self.freq} |
85 |
| - return json.dumps(metadata).encode() |
86 |
| - |
87 |
| - @classmethod |
88 |
| - def __arrow_ext_deserialize__(cls, storage_type, serialized) -> ArrowPeriodType: |
89 |
| - metadata = json.loads(serialized.decode()) |
90 |
| - return ArrowPeriodType(metadata["freq"]) |
91 |
| - |
92 |
| - def __eq__(self, other): |
93 |
| - if isinstance(other, pyarrow.BaseExtensionType): |
94 |
| - return type(self) == type(other) and self.freq == other.freq |
95 |
| - else: |
96 |
| - return NotImplemented |
97 |
| - |
98 |
| - def __hash__(self) -> int: |
99 |
| - return hash((str(self), self.freq)) |
100 |
| - |
101 |
| - def to_pandas_dtype(self): |
102 |
| - import pandas as pd |
103 |
| - |
104 |
| - return pd.PeriodDtype(freq=self.freq) |
105 |
| - |
106 |
| - |
107 |
| -# register the type with a dummy instance |
108 |
| -_period_type = ArrowPeriodType("D") |
109 |
| -pyarrow.register_extension_type(_period_type) |
110 |
| - |
111 |
| - |
112 |
| -class ArrowIntervalType(pyarrow.ExtensionType): |
113 |
| - @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
114 |
| - def __init__(self, subtype, inclusive: IntervalInclusiveType) -> None: |
115 |
| - # attributes need to be set first before calling |
116 |
| - # super init (as that calls serialize) |
117 |
| - assert inclusive in VALID_INCLUSIVE |
118 |
| - self._inclusive: IntervalInclusiveType = inclusive |
119 |
| - if not isinstance(subtype, pyarrow.DataType): |
120 |
| - subtype = pyarrow.type_for_alias(str(subtype)) |
121 |
| - self._subtype = subtype |
122 |
| - |
123 |
| - storage_type = pyarrow.struct([("left", subtype), ("right", subtype)]) |
124 |
| - pyarrow.ExtensionType.__init__(self, storage_type, "pandas.interval") |
125 |
| - |
126 |
| - @property |
127 |
| - def subtype(self): |
128 |
| - return self._subtype |
129 |
| - |
130 |
| - @property |
131 |
| - def inclusive(self) -> IntervalInclusiveType: |
132 |
| - return self._inclusive |
133 |
| - |
134 |
| - @property |
135 |
| - def closed(self) -> IntervalInclusiveType: |
136 |
| - warnings.warn( |
137 |
| - "Attribute `closed` is deprecated in favor of `inclusive`.", |
138 |
| - FutureWarning, |
139 |
| - stacklevel=find_stack_level(inspect.currentframe()), |
140 |
| - ) |
141 |
| - return self._inclusive |
142 |
| - |
143 |
| - def __arrow_ext_serialize__(self) -> bytes: |
144 |
| - metadata = {"subtype": str(self.subtype), "inclusive": self.inclusive} |
145 |
| - return json.dumps(metadata).encode() |
146 |
| - |
147 |
| - @classmethod |
148 |
| - def __arrow_ext_deserialize__(cls, storage_type, serialized) -> ArrowIntervalType: |
149 |
| - metadata = json.loads(serialized.decode()) |
150 |
| - subtype = pyarrow.type_for_alias(metadata["subtype"]) |
151 |
| - inclusive = metadata["inclusive"] |
152 |
| - return ArrowIntervalType(subtype, inclusive) |
153 |
| - |
154 |
| - def __eq__(self, other): |
155 |
| - if isinstance(other, pyarrow.BaseExtensionType): |
156 |
| - return ( |
157 |
| - type(self) == type(other) |
158 |
| - and self.subtype == other.subtype |
159 |
| - and self.inclusive == other.inclusive |
160 |
| - ) |
161 |
| - else: |
162 |
| - return NotImplemented |
163 |
| - |
164 |
| - def __hash__(self) -> int: |
165 |
| - return hash((str(self), str(self.subtype), self.inclusive)) |
166 |
| - |
167 |
| - def to_pandas_dtype(self): |
168 |
| - import pandas as pd |
169 |
| - |
170 |
| - return pd.IntervalDtype(self.subtype.to_pandas_dtype(), self.inclusive) |
171 |
| - |
172 |
| - |
173 |
| -# register the type with a dummy instance |
174 |
| -_interval_type = ArrowIntervalType(pyarrow.int64(), "left") |
175 |
| -pyarrow.register_extension_type(_interval_type) |
0 commit comments