Skip to content

Commit

Permalink
Merge branch 'master' into pandas2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jun 24, 2023
2 parents 89df3c2 + 44f0f9b commit a6b4b84
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci-check-dependencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Check dependency specification

on: [push, pull_request]

jobs:
test:
strategy:
matrix:
python-version: [3.9, "3.10", "3.11"]

runs-on: ubuntu-latest

env:
TEST_OPTS: "-rfsxEX -s"

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 100

- name: Get tags
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Get pip cache dir
id: pip-cache
run: echo "::set-output name=dir::$(pip cache dir)"

- name: Setup caching
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip-${{ matrix.python-version }}
restore-keys: |
pip-${{ matrix.python-version }}
- name: Install pint_pandas (just the bare minimum, no extras, no pre-installed dependencies)
run: |
pip install .
- name: Install pytest
run: pip install pytest

- name: Run Tests
run: |
pytest $TEST_OPTS
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pint-pandas Changelog
- Support for pandas 2.0, allowing `.cumsum, .cummax, .cummin` methods for `Series` and `DataFrame`. #186
- Minimum Pint version is 0.21
- Minimum Pandas vesrion is 2.0
- Support for unit registries with `force_ndarray_like = True`. #165

0.4 (2023-05-23)
----------------
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
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ classifiers = [
]
requires-python = ">=3.9"
dynamic = ["version"] # Version is taken from git tags using setuptools_scm
dependencies = [
"pint>=0.21",
"pandas>=1.5"
]

[tool.setuptools.package-data]
pint_pandas = [
"py.typed"
]
dependencies = [
"pint>=0.21",
"pandas>=1.5"
]

[project.optional-dependencies]
test = [
Expand Down

0 comments on commit a6b4b84

Please sign in to comment.