|
4 | 4 | import numpy as np
|
5 | 5 | import pandas as pd
|
6 | 6 | import pytest
|
| 7 | +import pint |
7 | 8 | from pandas.tests.extension.base.base import BaseExtensionTests
|
8 | 9 | from pint.testsuite import helpers
|
9 | 10 |
|
|
12 | 13 | ureg = PintType.ureg
|
13 | 14 |
|
14 | 15 |
|
| 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 | + |
15 | 53 | class TestIssue21(BaseExtensionTests):
|
16 | 54 | @pytest.mark.filterwarnings("ignore::RuntimeWarning")
|
17 | 55 | def test_offset_concat(self):
|
|
0 commit comments