Skip to content

Commit

Permalink
Merge #195
Browse files Browse the repository at this point in the history
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 <terji78@gmail.com>
  • Loading branch information
bors[bot] and topper-123 authored Aug 12, 2023
2 parents bf84e37 + 5909b5d commit 31a3055
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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(),
Expand All @@ -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()}
Expand Down Expand Up @@ -458,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:
Expand Down
2 changes: 1 addition & 1 deletion pint_pandas/testsuite/test_pandas_extensiontests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__"]:
Expand Down

0 comments on commit 31a3055

Please sign in to comment.