Skip to content

Commit 55f8e4e

Browse files
committed
Drop .util.series_of_pint_quantity()
The warning handled no longer appears with pint 0.24.4 and pandas 2.2.3.
1 parent 53018fc commit 55f8e4e

File tree

5 files changed

+8
-29
lines changed

5 files changed

+8
-29
lines changed

doc/api/util.rst

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Commonly used:
5454
private_data_path
5555
same_node
5656
same_time
57-
series_of_pint_quantity
5857
show_versions
5958

6059
.. automodule:: message_ix_models.util

message_ix_models/model/transport/ikarus.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package_data_path,
2020
same_node,
2121
same_time,
22-
series_of_pint_quantity,
2322
)
2423

2524
from .key import bcast_tcl, bcast_y
@@ -190,7 +189,7 @@ def read_ikarus_data(occupancy, k_output, k_inv_cost):
190189
output = registry.Quantity(
191190
occupancy[tec], "passenger / vehicle"
192191
) * k_output.get(tec, 1.0)
193-
df["output"] = series_of_pint_quantity([output] * len(df.index), index=df.index)
192+
df["output"] = pd.Series([output] * len(df.index), index=df.index)
194193

195194
df["inv_cost"] *= k_inv_cost.get(tec, 1.0)
196195

message_ix_models/tests/test_util.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
replace_par_data,
3838
same_node,
3939
same_time,
40-
series_of_pint_quantity,
4140
strip_par_data,
4241
)
4342

@@ -148,11 +147,11 @@ def test_check_support(test_context):
148147

149148

150149
def test_convert_units(recwarn):
151-
""":func:`.convert_units` and :func:`.series_of_pint_quantity` work."""
150+
""":func:`.convert_units` works."""
152151
# Common arguments
153152
args = [pd.Series([1.1, 10.2, 100.3], name="bar"), dict(bar=(10.0, "lb", "kg"))]
154153

155-
exp = series_of_pint_quantity(
154+
exp = pd.Series(
156155
[registry("4.9895 kg"), registry("46.2664 kg"), registry("454.9531 kg")],
157156
)
158157

@@ -168,12 +167,14 @@ def test_convert_units(recwarn):
168167
exp = pd.Series([q.magnitude for q in exp.values], name="bar")
169168
assert_series_equal(exp, convert_units(*args, store="magnitude"), check_dtype=False)
170169

170+
N = len(recwarn)
171+
171172
# Other values for store= are errors
172173
with pytest.raises(ValueError, match="store = 'foo'"):
173174
convert_units(*args, store="foo")
174175

175176
# series_of_pint_quantity() successfully caught warnings
176-
assert 0 == len(recwarn)
177+
assert N == len(recwarn)
177178

178179

179180
def test_copy_column():

message_ix_models/util/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pint
2121
from platformdirs import user_cache_path
2222

23-
from ._convert_units import convert_units, series_of_pint_quantity
23+
from ._convert_units import convert_units
2424
from ._logging import mark_time, preserve_log_level, silence_log
2525
from .cache import cached
2626
from .common import (
@@ -82,7 +82,6 @@
8282
"replace_par_data",
8383
"same_node",
8484
"same_time",
85-
"series_of_pint_quantity",
8685
"show_versions",
8786
"silence_log",
8887
"strip_par_data",

message_ix_models/util/_convert_units.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from collections.abc import Mapping
33
from functools import singledispatch
44
from typing import TYPE_CHECKING, Any, Optional
5-
from warnings import catch_warnings, filterwarnings
65

76
import pandas as pd
87
from iam_units import registry
@@ -13,24 +12,6 @@
1312
log = logging.getLogger(__name__)
1413

1514

16-
def series_of_pint_quantity(*args, **kwargs) -> pd.Series:
17-
"""Suppress a spurious warning.
18-
19-
Creating a :class:`pandas.Series` with a list of :class:`pint.Quantity` triggers a
20-
warning “The unit of the quantity is stripped when downcasting to ndarray,” even
21-
though the entire object is being stored and the unit is **not** stripped. This
22-
function suppresses this warning.
23-
"""
24-
with catch_warnings():
25-
filterwarnings(
26-
"ignore",
27-
message="The unit of the quantity is stripped when downcasting to ndarray",
28-
module="pandas.core.dtypes.cast",
29-
)
30-
31-
return pd.Series(*args, **kwargs)
32-
33-
3415
@singledispatch
3516
def convert_units(data: Any, **kwargs):
3617
"""Convert units of `data`.
@@ -85,7 +66,7 @@ def _(
8566
# - Reassemble into a series with index matching `s`
8667
result = registry.Quantity(factor * data.values, unit_in).to(unit_out)
8768

88-
return series_of_pint_quantity(
69+
return pd.Series(
8970
result.magnitude if store == "magnitude" else result.tolist(),
9071
index=data.index,
9172
dtype=(float if store == "magnitude" else object),

0 commit comments

Comments
 (0)