Skip to content

Commit

Permalink
Keep up with pandas21_compat changes
Browse files Browse the repository at this point in the history
Merge in important bugfixes for `na_value` handling.  And keep `black` happy.

Signed-off-by: Michael Tiemann <[email protected]>
  • Loading branch information
MichaelTiemannOSC committed Aug 12, 2023
1 parent 289c604 commit 602a804
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ def _values_for_factorize(self):
if HAS_UNCERTAINTIES and arr.size > 0:
# Canonicalize uncertain NaNs and pd.NA to np.nan
arr = arr.map(
lambda x: self.dtype.na_value.m if x is pd.NA or unp.isnan(x) else x
lambda x: self.dtype.na_value if x is pd.NA or unp.isnan(x) else x
)
return np.array(arr, copy=False), self.dtype.na_value.m
return np.array(arr, copy=False), self.dtype.na_value
return arr._values_for_factorize()

def value_counts(self, dropna=True):
Expand Down Expand Up @@ -631,7 +631,7 @@ def value_counts(self, dropna=True):
# compute counts on the data with no nans
data = self._data
nafilt = data.map(lambda x: x is pd.NA or unp.isnan(x))
na_value = self.dtype.na_value.m
na_value = self.dtype.na_value
data = data[~nafilt]
if HAS_UNCERTAINTIES and data.dtype.kind == "O":
unique_data = []
Expand Down Expand Up @@ -661,7 +661,7 @@ def unique(self):
from pandas import unique

data = self._data
na_value = self.dtype.na_value.m
na_value = self.dtype.na_value
if HAS_UNCERTAINTIES and data.dtype.kind == "O":
unique_data = []
for item in data:
Expand Down Expand Up @@ -824,7 +824,7 @@ def __array__(self, dtype=None, copy=False):
def _to_array_of_quantity(self, copy=False):
qtys = [
self._Q(item, self._dtype.units)
if item is not self.dtype.na_value.m
if item is not self.dtype.na_value
else self.dtype.na_value
for item in self._data
]
Expand Down
32 changes: 26 additions & 6 deletions pint_pandas/testsuite/test_pandas_extensiontests.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,18 @@ def _get_expected_exception(
if op_name in ["__pow__", "__rpow__"]:
return DimensionalityError
complex128_dtype = pd.core.dtypes.dtypes.NumpyEADtype("complex128")
if ((isinstance(obj, pd.Series) and obj.dtype == complex128_dtype)
or (isinstance(obj, pd.DataFrame) and any([dtype == complex128_dtype for dtype in obj.dtypes]))
if (
(isinstance(obj, pd.Series) and obj.dtype == complex128_dtype)
or (
isinstance(obj, pd.DataFrame)
and any([dtype == complex128_dtype for dtype in obj.dtypes])
)
or (isinstance(other, pd.Series) and other.dtype == complex128_dtype)
or (isinstance(other, pd.DataFrame) and any([dtype == complex128_dtype for dtype in other.dtypes]))):
or (
isinstance(other, pd.DataFrame)
and any([dtype == complex128_dtype for dtype in other.dtypes])
)
):
if op_name in ["__floordiv__", "__rfloordiv__", "__mod__", "__rmod__"]:
return TypeError
return super()._get_expected_exception(op_name, obj, other)
Expand Down Expand Up @@ -611,7 +619,11 @@ class TestMissing(base.BaseMissingTests):
class TestNumericReduce(base.BaseNumericReduceTests):
def _supports_reduction(self, obj, op_name: str) -> bool:
# Specify if we expect this reduction to succeed.
if USE_UNCERTAINTIES and op_name in _all_numeric_reductions and op_name not in _uncertain_numeric_reductions:
if (
USE_UNCERTAINTIES
and op_name in _all_numeric_reductions
and op_name not in _uncertain_numeric_reductions
):
if any([isinstance(v, UFloat) for v in obj.values.quantity._magnitude]):
pytest.skip(f"reduction {op_name} not implemented in uncertainties")
return super()._supports_reduction(obj, op_name)
Expand Down Expand Up @@ -646,7 +658,11 @@ def test_reduce_scaling(
This verifies that the result units are sensible.
"""
op_name = all_numeric_reductions
if USE_UNCERTAINTIES and op_name in _all_numeric_reductions and op_name not in _uncertain_numeric_reductions:
if (
USE_UNCERTAINTIES
and op_name in _all_numeric_reductions
and op_name not in _uncertain_numeric_reductions
):
if any([isinstance(v, UFloat) for v in data.quantity._magnitude]):
pytest.skip(f"reduction {op_name} not implemented in uncertainties")
s_nm = pd.Series(data)
Expand Down Expand Up @@ -683,7 +699,11 @@ def test_reduce_series(
self, data, all_numeric_reductions, skipna, USE_UNCERTAINTIES
):
op_name = all_numeric_reductions
if USE_UNCERTAINTIES and op_name in _all_numeric_reductions and op_name not in _uncertain_numeric_reductions:
if (
USE_UNCERTAINTIES
and op_name in _all_numeric_reductions
and op_name not in _uncertain_numeric_reductions
):
if any([isinstance(v, UFloat) for v in data.quantity._magnitude]):
pytest.skip(f"reduction {op_name} not implemented in uncertainties")
s = pd.Series(data)
Expand Down

0 comments on commit 602a804

Please sign in to comment.