Skip to content

Commit 88bea62

Browse files
authored
REF: move PandasDtype to dtypes.dtypes (#39385)
1 parent 8d7ff93 commit 88bea62

File tree

8 files changed

+126
-110
lines changed

8 files changed

+126
-110
lines changed

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
needs_i8_conversion,
4848
pandas_dtype,
4949
)
50+
from pandas.core.dtypes.dtypes import PandasDtype
5051
from pandas.core.dtypes.generic import (
5152
ABCDatetimeArray,
5253
ABCExtensionArray,
@@ -1929,7 +1930,6 @@ def diff(arr, n: int, axis: int = 0, stacklevel=3):
19291930
-------
19301931
shifted
19311932
"""
1932-
from pandas.core.arrays import PandasDtype
19331933

19341934
n = int(n)
19351935
na = np.nan
@@ -1942,7 +1942,7 @@ def diff(arr, n: int, axis: int = 0, stacklevel=3):
19421942

19431943
if isinstance(dtype, PandasDtype):
19441944
# PandasArray cannot necessarily hold shifted versions of itself.
1945-
arr = np.asarray(arr)
1945+
arr = arr.to_numpy()
19461946
dtype = arr.dtype
19471947

19481948
if is_extension_array_dtype(dtype):

pandas/core/arrays/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.core.arrays.integer import IntegerArray
1111
from pandas.core.arrays.interval import IntervalArray
1212
from pandas.core.arrays.masked import BaseMaskedArray
13-
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
13+
from pandas.core.arrays.numpy_ import PandasArray
1414
from pandas.core.arrays.period import PeriodArray, period_array
1515
from pandas.core.arrays.sparse import SparseArray
1616
from pandas.core.arrays.string_ import StringArray
@@ -28,7 +28,6 @@
2828
"IntegerArray",
2929
"IntervalArray",
3030
"PandasArray",
31-
"PandasDtype",
3231
"PeriodArray",
3332
"period_array",
3433
"SparseArray",

pandas/core/arrays/numpy_.py

+2-97
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import numbers
4-
from typing import Optional, Tuple, Type, Union
4+
from typing import Optional, Tuple, Union
55

66
import numpy as np
77
from numpy.lib.mixins import NDArrayOperatorsMixin
@@ -10,7 +10,7 @@
1010
from pandas._typing import Dtype, NpDtype, Scalar
1111
from pandas.compat.numpy import function as nv
1212

13-
from pandas.core.dtypes.dtypes import ExtensionDtype
13+
from pandas.core.dtypes.dtypes import PandasDtype
1414
from pandas.core.dtypes.missing import isna
1515

1616
from pandas.core import nanops, ops
@@ -19,101 +19,6 @@
1919
from pandas.core.strings.object_array import ObjectStringArrayMixin
2020

2121

22-
class PandasDtype(ExtensionDtype):
23-
"""
24-
A Pandas ExtensionDtype for NumPy dtypes.
25-
26-
.. versionadded:: 0.24.0
27-
28-
This is mostly for internal compatibility, and is not especially
29-
useful on its own.
30-
31-
Parameters
32-
----------
33-
dtype : object
34-
Object to be converted to a NumPy data type object.
35-
36-
See Also
37-
--------
38-
numpy.dtype
39-
"""
40-
41-
_metadata = ("_dtype",)
42-
43-
def __init__(self, dtype: Optional[NpDtype]):
44-
self._dtype = np.dtype(dtype)
45-
46-
def __repr__(self) -> str:
47-
return f"PandasDtype({repr(self.name)})"
48-
49-
@property
50-
def numpy_dtype(self) -> np.dtype:
51-
"""
52-
The NumPy dtype this PandasDtype wraps.
53-
"""
54-
return self._dtype
55-
56-
@property
57-
def name(self) -> str:
58-
"""
59-
A bit-width name for this data-type.
60-
"""
61-
return self._dtype.name
62-
63-
@property
64-
def type(self) -> Type[np.generic]:
65-
"""
66-
The type object used to instantiate a scalar of this NumPy data-type.
67-
"""
68-
return self._dtype.type
69-
70-
@property
71-
def _is_numeric(self) -> bool:
72-
# exclude object, str, unicode, void.
73-
return self.kind in set("biufc")
74-
75-
@property
76-
def _is_boolean(self) -> bool:
77-
return self.kind == "b"
78-
79-
@classmethod
80-
def construct_from_string(cls, string: str) -> PandasDtype:
81-
try:
82-
dtype = np.dtype(string)
83-
except TypeError as err:
84-
if not isinstance(string, str):
85-
msg = f"'construct_from_string' expects a string, got {type(string)}"
86-
else:
87-
msg = f"Cannot construct a 'PandasDtype' from '{string}'"
88-
raise TypeError(msg) from err
89-
return cls(dtype)
90-
91-
@classmethod
92-
def construct_array_type(cls) -> Type[PandasArray]:
93-
"""
94-
Return the array type associated with this dtype.
95-
96-
Returns
97-
-------
98-
type
99-
"""
100-
return PandasArray
101-
102-
@property
103-
def kind(self) -> str:
104-
"""
105-
A character code (one of 'biufcmMOSUV') identifying the general kind of data.
106-
"""
107-
return self._dtype.kind
108-
109-
@property
110-
def itemsize(self) -> int:
111-
"""
112-
The element size of this data-type object.
113-
"""
114-
return self._dtype.itemsize
115-
116-
11722
class PandasArray(
11823
OpsMixin,
11924
NDArrayBackedExtensionArray,

pandas/core/dtypes/dtypes.py

+107-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
to_offset,
3232
tz_compare,
3333
)
34-
from pandas._typing import Dtype, DtypeObj, Ordered
34+
from pandas._typing import Dtype, DtypeObj, NpDtype, Ordered
3535

3636
from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype
3737
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndex
@@ -41,7 +41,12 @@
4141
import pyarrow
4242

4343
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+
)
4550

4651
str_type = str
4752

@@ -1230,3 +1235,103 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
12301235
if common == object:
12311236
return np.dtype(object)
12321237
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

pandas/core/internals/array_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
is_extension_array_dtype,
1919
is_numeric_dtype,
2020
)
21-
from pandas.core.dtypes.dtypes import ExtensionDtype
21+
from pandas.core.dtypes.dtypes import ExtensionDtype, PandasDtype
2222
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
2323
from pandas.core.dtypes.missing import isna
2424

2525
import pandas.core.algorithms as algos
26-
from pandas.core.arrays import ExtensionArray, PandasDtype
26+
from pandas.core.arrays import ExtensionArray
2727
from pandas.core.arrays.sparse import SparseDtype
2828
from pandas.core.construction import extract_array
2929
from pandas.core.indexers import maybe_convert_indices

pandas/core/internals/blocks.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
is_sparse,
5151
pandas_dtype,
5252
)
53-
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype
53+
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype, PandasDtype
5454
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndex, ABCPandasArray, ABCSeries
5555
from pandas.core.dtypes.missing import isna
5656

@@ -67,7 +67,6 @@
6767
DatetimeArray,
6868
ExtensionArray,
6969
PandasArray,
70-
PandasDtype,
7170
TimedeltaArray,
7271
)
7372
from pandas.core.base import PandasObject

pandas/tests/arrays/test_numpy.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import numpy as np
66
import pytest
77

8+
from pandas.core.dtypes.dtypes import PandasDtype
9+
810
import pandas as pd
911
import pandas._testing as tm
1012
from pandas.arrays import PandasArray
11-
from pandas.core.arrays.numpy_ import PandasDtype
1213

1314

1415
@pytest.fixture(
@@ -86,6 +87,13 @@ def test_constructor_from_string():
8687
assert result == expected
8788

8889

90+
def test_dtype_univalent(any_numpy_dtype):
91+
dtype = PandasDtype(any_numpy_dtype)
92+
93+
result = PandasDtype(dtype)
94+
assert result == dtype
95+
96+
8997
# ----------------------------------------------------------------------------
9098
# Construction
9199

pandas/tests/extension/test_numpy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import numpy as np
1717
import pytest
1818

19-
from pandas.core.dtypes.dtypes import ExtensionDtype
19+
from pandas.core.dtypes.dtypes import ExtensionDtype, PandasDtype
2020

2121
import pandas as pd
2222
import pandas._testing as tm
23-
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
23+
from pandas.core.arrays.numpy_ import PandasArray
2424

2525
from . import base
2626

0 commit comments

Comments
 (0)