From 52c43287821a6f3470fa42e56b42919b4d4ee0b9 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Wed, 30 Aug 2023 20:04:47 +0200 Subject: [PATCH] Address FutureWarnings from pandas 2.1.0 --- genno/core/attrseries.py | 4 ++-- genno/tests/compat/test_pyam.py | 5 +++++ genno/util.py | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/genno/core/attrseries.py b/genno/core/attrseries.py index 541a4846..7ece6097 100644 --- a/genno/core/attrseries.py +++ b/genno/core/attrseries.py @@ -186,7 +186,7 @@ def bfill(self, dim: Hashable, limit: Optional[int] = None): # if needed use _maybe_groupby() return self._replace( self.unstack(dim) - .fillna(method="bfill", axis=1, limit=limit) + .bfill(axis=1, limit=limit) .stack() .reorder_levels(self.dims), ) @@ -271,7 +271,7 @@ def ffill(self, dim: Hashable, limit: Optional[int] = None): # if needed use _maybe_groupby() return self._replace( self.unstack(dim) - .fillna(method="ffill", axis=1, limit=limit) + .ffill(axis=1, limit=limit) .stack() .reorder_levels(self.dims), ) diff --git a/genno/tests/compat/test_pyam.py b/genno/tests/compat/test_pyam.py index febe3014..72cd1dd4 100644 --- a/genno/tests/compat/test_pyam.py +++ b/genno/tests/compat/test_pyam.py @@ -14,6 +14,11 @@ # Skip this entire file if pyam is not installed pyam = pytest.importorskip("pyam", reason="pyam-iamc not installed") +# Warning emitted by pandas ≥ 2.1.0 with pyam 1.9.0 +pytestmark = pytest.mark.filterwarnings( + "ignore:.*unique with argument that is not.*:FutureWarning:pyam.core" +) + @pytest.fixture(scope="session") def scenario(): diff --git a/genno/util.py b/genno/util.py index 32dcb76c..23bf3ef7 100644 --- a/genno/util.py +++ b/genno/util.py @@ -3,6 +3,7 @@ from inspect import Parameter, signature from typing import Callable, Iterable, Mapping, MutableMapping, Tuple, Type, Union +import numpy as np import pandas as pd import pint from dask.core import literal @@ -95,6 +96,12 @@ def parse_units(data: Iterable, registry=None) -> pint.Unit: """ registry = registry or pint.get_application_registry() + # Ensure a type that is accepted by pd.unique() + if isinstance(data, str): + data = np.array([data]) + elif not isinstance(data, (np.ndarray, pd.Index, pd.Series)): + data = np.array(data) + unit = pd.unique(data) if len(unit) > 1: