From 602a804bf9a00da63d3b7ba55554fa6aa9ccc924 Mon Sep 17 00:00:00 2001 From: Michael Tiemann <72577720+MichaelTiemannOSC@users.noreply.github.com> Date: Fri, 11 Aug 2023 23:33:50 -0400 Subject: [PATCH] Keep up with pandas21_compat changes Merge in important bugfixes for `na_value` handling. And keep `black` happy. Signed-off-by: Michael Tiemann <72577720+MichaelTiemannOSC@users.noreply.github.com> --- pint_pandas/pint_array.py | 10 +++--- .../testsuite/test_pandas_extensiontests.py | 32 +++++++++++++++---- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index e75cb5c..6695b7d 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -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): @@ -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 = [] @@ -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: @@ -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 ] diff --git a/pint_pandas/testsuite/test_pandas_extensiontests.py b/pint_pandas/testsuite/test_pandas_extensiontests.py index d7060a4..672de1c 100644 --- a/pint_pandas/testsuite/test_pandas_extensiontests.py +++ b/pint_pandas/testsuite/test_pandas_extensiontests.py @@ -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) @@ -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) @@ -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) @@ -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)