|
31 | 31 | to_offset,
|
32 | 32 | tz_compare,
|
33 | 33 | )
|
34 |
| -from pandas._typing import Dtype, DtypeObj, Ordered |
| 34 | +from pandas._typing import Dtype, DtypeObj, NpDtype, Ordered |
35 | 35 |
|
36 | 36 | from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype
|
37 | 37 | from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndex
|
|
41 | 41 | import pyarrow
|
42 | 42 |
|
43 | 43 | from pandas import Categorical
|
44 |
| - from pandas.core.arrays import DatetimeArray, IntervalArray, PeriodArray |
| 44 | + from pandas.core.arrays import ( |
| 45 | + DatetimeArray, |
| 46 | + IntervalArray, |
| 47 | + PandasArray, |
| 48 | + PeriodArray, |
| 49 | + ) |
45 | 50 |
|
46 | 51 | str_type = str
|
47 | 52 |
|
@@ -1230,3 +1235,103 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
|
1230 | 1235 | if common == object:
|
1231 | 1236 | return np.dtype(object)
|
1232 | 1237 | return IntervalDtype(common, closed=closed)
|
| 1238 | + |
| 1239 | + |
| 1240 | +class PandasDtype(ExtensionDtype): |
| 1241 | + """ |
| 1242 | + A Pandas ExtensionDtype for NumPy dtypes. |
| 1243 | +
|
| 1244 | + .. versionadded:: 0.24.0 |
| 1245 | +
|
| 1246 | + This is mostly for internal compatibility, and is not especially |
| 1247 | + useful on its own. |
| 1248 | +
|
| 1249 | + Parameters |
| 1250 | + ---------- |
| 1251 | + dtype : object |
| 1252 | + Object to be converted to a NumPy data type object. |
| 1253 | +
|
| 1254 | + See Also |
| 1255 | + -------- |
| 1256 | + numpy.dtype |
| 1257 | + """ |
| 1258 | + |
| 1259 | + _metadata = ("_dtype",) |
| 1260 | + |
| 1261 | + def __init__(self, dtype: Optional[Union[NpDtype, PandasDtype]]): |
| 1262 | + if isinstance(dtype, PandasDtype): |
| 1263 | + # make constructor univalent |
| 1264 | + dtype = dtype.numpy_dtype |
| 1265 | + self._dtype = np.dtype(dtype) |
| 1266 | + |
| 1267 | + def __repr__(self) -> str: |
| 1268 | + return f"PandasDtype({repr(self.name)})" |
| 1269 | + |
| 1270 | + @property |
| 1271 | + def numpy_dtype(self) -> np.dtype: |
| 1272 | + """ |
| 1273 | + The NumPy dtype this PandasDtype wraps. |
| 1274 | + """ |
| 1275 | + return self._dtype |
| 1276 | + |
| 1277 | + @property |
| 1278 | + def name(self) -> str: |
| 1279 | + """ |
| 1280 | + A bit-width name for this data-type. |
| 1281 | + """ |
| 1282 | + return self._dtype.name |
| 1283 | + |
| 1284 | + @property |
| 1285 | + def type(self) -> Type[np.generic]: |
| 1286 | + """ |
| 1287 | + The type object used to instantiate a scalar of this NumPy data-type. |
| 1288 | + """ |
| 1289 | + return self._dtype.type |
| 1290 | + |
| 1291 | + @property |
| 1292 | + def _is_numeric(self) -> bool: |
| 1293 | + # exclude object, str, unicode, void. |
| 1294 | + return self.kind in set("biufc") |
| 1295 | + |
| 1296 | + @property |
| 1297 | + def _is_boolean(self) -> bool: |
| 1298 | + return self.kind == "b" |
| 1299 | + |
| 1300 | + @classmethod |
| 1301 | + def construct_from_string(cls, string: str) -> PandasDtype: |
| 1302 | + try: |
| 1303 | + dtype = np.dtype(string) |
| 1304 | + except TypeError as err: |
| 1305 | + if not isinstance(string, str): |
| 1306 | + msg = f"'construct_from_string' expects a string, got {type(string)}" |
| 1307 | + else: |
| 1308 | + msg = f"Cannot construct a 'PandasDtype' from '{string}'" |
| 1309 | + raise TypeError(msg) from err |
| 1310 | + return cls(dtype) |
| 1311 | + |
| 1312 | + @classmethod |
| 1313 | + def construct_array_type(cls) -> Type[PandasArray]: |
| 1314 | + """ |
| 1315 | + Return the array type associated with this dtype. |
| 1316 | +
|
| 1317 | + Returns |
| 1318 | + ------- |
| 1319 | + type |
| 1320 | + """ |
| 1321 | + from pandas.core.arrays import PandasArray |
| 1322 | + |
| 1323 | + return PandasArray |
| 1324 | + |
| 1325 | + @property |
| 1326 | + def kind(self) -> str: |
| 1327 | + """ |
| 1328 | + A character code (one of 'biufcmMOSUV') identifying the general kind of data. |
| 1329 | + """ |
| 1330 | + return self._dtype.kind |
| 1331 | + |
| 1332 | + @property |
| 1333 | + def itemsize(self) -> int: |
| 1334 | + """ |
| 1335 | + The element size of this data-type object. |
| 1336 | + """ |
| 1337 | + return self._dtype.itemsize |
0 commit comments