From 9876d88626e3aaa680a2a44d0226feea54579331 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Tue, 1 Aug 2023 14:13:16 +0100 Subject: [PATCH 1/2] fix pandas v2.1 NumpyEADtype issue --- pint_pandas/pint_array.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 2b5c40f..2af2c86 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -2,6 +2,7 @@ import re import warnings from collections import OrderedDict +from importlib.metadata import version import numpy as np import pandas as pd @@ -27,6 +28,11 @@ # quantify/dequantify NO_UNIT = "No Unit" +pandas_version = version("pandas") +pandas_version_info = tuple( + int(x) if x.isdigit() else x for x in pandas_version.split(".") +) + class PintType(ExtensionDtype): """ @@ -185,6 +191,12 @@ def __repr__(self): return self.name +_NumpyEADtype = ( + pd.core.dtypes.dtypes.PandasDtype + if pandas_version_info < (2, 1) + else pd.core.dtypes.dtypes.NumpyEADtype +) + dtypemap = { int: pd.Int64Dtype(), np.int64: pd.Int64Dtype(), @@ -195,8 +207,8 @@ def __repr__(self): float: pd.Float64Dtype(), np.float64: pd.Float64Dtype(), np.float32: pd.Float32Dtype(), - np.complex128: pd.core.dtypes.dtypes.PandasDtype("complex128"), - np.complex64: pd.core.dtypes.dtypes.PandasDtype("complex64"), + np.complex128: _NumpyEADtype("complex128"), + np.complex64: _NumpyEADtype("complex64"), # np.float16: pd.Float16Dtype(), } dtypeunmap = {v: k for k, v in dtypemap.items()} From 5909b5dd0b811ea8262888b94fc194279b97f693 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Tue, 1 Aug 2023 15:00:43 +0100 Subject: [PATCH 2/2] fix pandas v2.1 issues, various --- pint_pandas/pint_array.py | 3 ++- pint_pandas/testsuite/test_pandas_extensiontests.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 2af2c86..c795904 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -470,7 +470,8 @@ def take(self, indices, allow_fill=False, fill_value=None): Examples -------- """ - from pandas.core.algorithms import take, is_scalar + from pandas.core.algorithms import take + from pandas.api.types import is_scalar data = self._data if allow_fill and fill_value is None: diff --git a/pint_pandas/testsuite/test_pandas_extensiontests.py b/pint_pandas/testsuite/test_pandas_extensiontests.py index 698cbb5..b85ed69 100644 --- a/pint_pandas/testsuite/test_pandas_extensiontests.py +++ b/pint_pandas/testsuite/test_pandas_extensiontests.py @@ -343,7 +343,7 @@ def _check_divmod_op(self, s, op, other, exc=None): divmod(s, other) def _get_exception(self, data, op_name): - if data.data.dtype == pd.core.dtypes.dtypes.PandasDtype("complex128"): + if data.data.dtype == dtypemap[np.complex128]: if op_name in ["__floordiv__", "__rfloordiv__", "__mod__", "__rmod__"]: return op_name, TypeError if op_name in ["__pow__", "__rpow__"]: