Skip to content

Commit 31a3055

Browse files
Merge #195
195: fix pandas v2.1 NumpyEADtype issue r=andrewgsavage a=topper-123 - [x] Closes #194 - [x] Executed `pre-commit run --all-files` with no errors - [x] The change is fully covered by automated unit tests - [ ] Documented in docs/ as appropriate - [ ] Added an entry to the CHANGES file Precursor to working on #174. Co-authored-by: Terji Petersen <[email protected]>
2 parents bf84e37 + 5909b5d commit 31a3055

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pint_pandas/pint_array.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import warnings
44
from collections import OrderedDict
5+
from importlib.metadata import version
56

67
import numpy as np
78
import pandas as pd
@@ -27,6 +28,11 @@
2728
# quantify/dequantify
2829
NO_UNIT = "No Unit"
2930

31+
pandas_version = version("pandas")
32+
pandas_version_info = tuple(
33+
int(x) if x.isdigit() else x for x in pandas_version.split(".")
34+
)
35+
3036

3137
class PintType(ExtensionDtype):
3238
"""
@@ -185,6 +191,12 @@ def __repr__(self):
185191
return self.name
186192

187193

194+
_NumpyEADtype = (
195+
pd.core.dtypes.dtypes.PandasDtype
196+
if pandas_version_info < (2, 1)
197+
else pd.core.dtypes.dtypes.NumpyEADtype
198+
)
199+
188200
dtypemap = {
189201
int: pd.Int64Dtype(),
190202
np.int64: pd.Int64Dtype(),
@@ -195,8 +207,8 @@ def __repr__(self):
195207
float: pd.Float64Dtype(),
196208
np.float64: pd.Float64Dtype(),
197209
np.float32: pd.Float32Dtype(),
198-
np.complex128: pd.core.dtypes.dtypes.PandasDtype("complex128"),
199-
np.complex64: pd.core.dtypes.dtypes.PandasDtype("complex64"),
210+
np.complex128: _NumpyEADtype("complex128"),
211+
np.complex64: _NumpyEADtype("complex64"),
200212
# np.float16: pd.Float16Dtype(),
201213
}
202214
dtypeunmap = {v: k for k, v in dtypemap.items()}
@@ -458,7 +470,8 @@ def take(self, indices, allow_fill=False, fill_value=None):
458470
Examples
459471
--------
460472
"""
461-
from pandas.core.algorithms import take, is_scalar
473+
from pandas.core.algorithms import take
474+
from pandas.api.types import is_scalar
462475

463476
data = self._data
464477
if allow_fill and fill_value is None:

pint_pandas/testsuite/test_pandas_extensiontests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def _check_divmod_op(self, s, op, other, exc=None):
343343
divmod(s, other)
344344

345345
def _get_exception(self, data, op_name):
346-
if data.data.dtype == pd.core.dtypes.dtypes.PandasDtype("complex128"):
346+
if data.data.dtype == dtypemap[np.complex128]:
347347
if op_name in ["__floordiv__", "__rfloordiv__", "__mod__", "__rmod__"]:
348348
return op_name, TypeError
349349
if op_name in ["__pow__", "__rpow__"]:

0 commit comments

Comments
 (0)