Skip to content

Commit

Permalink
Merge #187
Browse files Browse the repository at this point in the history
187: Bugfix/fix 165 fill value must be scalar r=andrewgsavage a=burnpanck

- [x] Closes #165
- [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~
- [x] Added an entry to the CHANGES file

Co-authored-by: Yves Delley <[email protected]>
  • Loading branch information
bors[bot] and burnpanck committed Jun 22, 2023
2 parents 62971df + f93304e commit 44f0f9b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pint-pandas Changelog
- Support for magnitudes of any type, such as complex128 or tuples #146
- Support for Pint 0.21 #168, #179
- Cast to `numpy.ndarray` in `PintArray._reduce` if needed to use `nanops` functions
- Support for unit registries with `force_ndarray_like = True`. #165

0.3 (2022-11-14)
----------------
Expand Down
6 changes: 5 additions & 1 deletion pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,17 @@ def take(self, indices, allow_fill=False, fill_value=None):
Examples
--------
"""
from pandas.core.algorithms import take
from pandas.core.algorithms import take, is_scalar

data = self._data
if allow_fill and fill_value is None:
fill_value = self.dtype.na_value
if isinstance(fill_value, _Quantity):
fill_value = fill_value.to(self.units).magnitude
if not is_scalar(fill_value) and not fill_value.ndim:
# deal with Issue #165; for unit registries with force_ndarray_like = True,
# magnitude is in fact an array scalar, which will get rejected by pandas.
fill_value = fill_value[()]

result = take(data, indices, fill_value=fill_value, allow_fill=allow_fill)

Expand Down
38 changes: 38 additions & 0 deletions pint_pandas/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
import pytest
import pint
from pandas.tests.extension.base.base import BaseExtensionTests
from pint.testsuite import helpers

Expand All @@ -12,6 +13,43 @@
ureg = PintType.ureg


class TestIssue165(BaseExtensionTests):
def test_force_ndarray_like(self):
# store previous registries to undo our changes
prev_PintType_ureg = PintType.ureg
prev_appreg = pint.get_application_registry().get()
prev_cache = PintType._cache
try:
# create a temporary registry with force_ndarray_like = True (`pint_xarray` insists on that)
test_ureg = pint.UnitRegistry()
test_ureg.force_ndarray_like = True
# register
pint.set_application_registry(test_ureg)
PintType.ureg = test_ureg
# clear units cache
PintType._cache = {}

# run TestIssue21.test_offset_concat with our test-registry (one of many that currently fails with force_ndarray_like=True)
q_a = ureg.Quantity(np.arange(5), test_ureg.Unit("degC"))
q_b = ureg.Quantity(np.arange(6), test_ureg.Unit("degC"))
q_a_ = np.append(q_a, np.nan)

a = pd.Series(PintArray(q_a))
b = pd.Series(PintArray(q_b))

result = pd.concat([a, b], axis=1)
expected = pd.DataFrame(
{0: PintArray(q_a_), 1: PintArray(q_b)}, dtype="pint[degC]"
)
self.assert_equal(result, expected)

finally:
# restore registry
PintType.ureg = prev_PintType_ureg
PintType._cache = prev_cache
pint.set_application_registry(prev_appreg)


class TestIssue21(BaseExtensionTests):
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_offset_concat(self):
Expand Down

0 comments on commit 44f0f9b

Please sign in to comment.