Skip to content

Commit 4ff2ec4

Browse files
Fix value_counts and other feedback
Fix errors that crept into value_counts concerning na_type. And canonicalize na_value in quantities a bit more generally. As stated in a previous commit comment, if we leave naked NA/NaNs in our Quantity array, the code gracefully handles unit addition/subtraction, but unit multiplication/division loses. Signed-off-by: Michael Tiemann <[email protected]>
1 parent 57fc253 commit 4ff2ec4

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

pint_pandas/pint_array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def value_counts(self, dropna=True):
598598
# compute counts on the data with no nans
599599
data = self._data
600600
nafilt = pd.isna(data)
601-
na_value = self.dtype.na_value
601+
na_value = pd.NA # NA value for index, not data, so not quantified
602602
data = data[~nafilt]
603603
index = list(set(data))
604604

@@ -607,9 +607,9 @@ def value_counts(self, dropna=True):
607607

608608
if not dropna:
609609
index.append(na_value)
610-
array.append(len(nafilt))
610+
array.append(nafilt.sum())
611611

612-
return Series(array, index=index)
612+
return Series(np.asarray(array), index=index)
613613

614614
def unique(self):
615615
"""Compute the PintArray of unique values.
@@ -773,7 +773,7 @@ def __array__(self, dtype=None, copy=False):
773773
def _to_array_of_quantity(self, copy=False):
774774
qtys = [
775775
self._Q(item, self._dtype.units)
776-
if item is not self.dtype.na_value
776+
if not pd.isna(item)
777777
else self.dtype.na_value
778778
for item in self._data
779779
]

pint_pandas/testsuite/test_pandas_extensiontests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ def test_groupby_extension_no_sort(self, data_for_grouping):
316316
tm.assert_series_equal(result, expected)
317317

318318

319+
class TestInterface(base.BaseInterfaceTests):
320+
pass
321+
322+
319323
class TestMethods(base.BaseMethodsTests):
320324
def test_apply_simple_series(self, data):
321325
result = pd.Series(data).apply(lambda x: x * 2 + ureg.Quantity(1, x.u))
@@ -537,10 +541,7 @@ def test_reduce_scaling(self, data, all_numeric_reductions, skipna):
537541
# min/max with empty produce numpy warnings
538542
with warnings.catch_warnings():
539543
warnings.simplefilter("ignore", RuntimeWarning)
540-
# try:
541544
r_nm = getattr(s_nm, op_name)(skipna=skipna)
542-
# except AttributeError:
543-
# pytest.skip("bye!")
544545
r_mm = getattr(s_mm, op_name)(skipna=skipna)
545546
if isinstance(r_nm, ureg.Quantity):
546547
# convert both results to the same units, then take the magnitude

0 commit comments

Comments
 (0)