Skip to content

Commit 44f0f9b

Browse files
bors[bot]burnpanck
andauthored
187: Bugfix/fix 165 fill value must be scalar r=andrewgsavage a=burnpanck - [x] Closes hgrecco#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]>
2 parents 62971df + f93304e commit 44f0f9b

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pint-pandas Changelog
88
- Support for magnitudes of any type, such as complex128 or tuples #146
99
- Support for Pint 0.21 #168, #179
1010
- Cast to `numpy.ndarray` in `PintArray._reduce` if needed to use `nanops` functions
11+
- Support for unit registries with `force_ndarray_like = True`. #165
1112

1213
0.3 (2022-11-14)
1314
----------------

pint_pandas/pint_array.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,17 @@ def take(self, indices, allow_fill=False, fill_value=None):
458458
Examples
459459
--------
460460
"""
461-
from pandas.core.algorithms import take
461+
from pandas.core.algorithms import take, is_scalar
462462

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

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

pint_pandas/testsuite/test_issues.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import pandas as pd
66
import pytest
7+
import pint
78
from pandas.tests.extension.base.base import BaseExtensionTests
89
from pint.testsuite import helpers
910

@@ -12,6 +13,43 @@
1213
ureg = PintType.ureg
1314

1415

16+
class TestIssue165(BaseExtensionTests):
17+
def test_force_ndarray_like(self):
18+
# store previous registries to undo our changes
19+
prev_PintType_ureg = PintType.ureg
20+
prev_appreg = pint.get_application_registry().get()
21+
prev_cache = PintType._cache
22+
try:
23+
# create a temporary registry with force_ndarray_like = True (`pint_xarray` insists on that)
24+
test_ureg = pint.UnitRegistry()
25+
test_ureg.force_ndarray_like = True
26+
# register
27+
pint.set_application_registry(test_ureg)
28+
PintType.ureg = test_ureg
29+
# clear units cache
30+
PintType._cache = {}
31+
32+
# run TestIssue21.test_offset_concat with our test-registry (one of many that currently fails with force_ndarray_like=True)
33+
q_a = ureg.Quantity(np.arange(5), test_ureg.Unit("degC"))
34+
q_b = ureg.Quantity(np.arange(6), test_ureg.Unit("degC"))
35+
q_a_ = np.append(q_a, np.nan)
36+
37+
a = pd.Series(PintArray(q_a))
38+
b = pd.Series(PintArray(q_b))
39+
40+
result = pd.concat([a, b], axis=1)
41+
expected = pd.DataFrame(
42+
{0: PintArray(q_a_), 1: PintArray(q_b)}, dtype="pint[degC]"
43+
)
44+
self.assert_equal(result, expected)
45+
46+
finally:
47+
# restore registry
48+
PintType.ureg = prev_PintType_ureg
49+
PintType._cache = prev_cache
50+
pint.set_application_registry(prev_appreg)
51+
52+
1553
class TestIssue21(BaseExtensionTests):
1654
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
1755
def test_offset_concat(self):

0 commit comments

Comments
 (0)