Skip to content

Commit 4b9f2db

Browse files
authored
Make emergent constraints diagnostics compatible with Pandas>=3 (#4356)
1 parent fe64d7e commit 4b9f2db

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

  • esmvaltool/diag_scripts/emergent_constraints

esmvaltool/diag_scripts/emergent_constraints/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,14 @@ def _create_scatterplot(
523523
for idx, _ in enumerate(x_data):
524524
if numbers_as_markers:
525525
axes.text(
526-
x_data[idx],
527-
y_data[idx],
526+
x_data.iloc[idx],
527+
y_data.iloc[idx],
528528
x_data.index.get_level_values(-1)[idx],
529529
size=7,
530530
**scatter_kwargs,
531531
)
532532
else:
533-
axes.scatter(x_data[idx], y_data[idx], **scatter_kwargs)
533+
axes.scatter(x_data.iloc[idx], y_data.iloc[idx], **scatter_kwargs)
534534

535535
# Regression line
536536
line_kwargs = {**kwargs, "linestyle": "-"}
@@ -635,6 +635,18 @@ def _create_regplot(
635635

636636
def _get_pandas_cube(pandas_object):
637637
"""Convert :mod:`pandas` object to cube and fix coordinates."""
638+
# Make sure dtype of indices and columns is "object" (just like it used to
639+
# be in Pandas < 3) because iris.pandas.as_cube cannot handle the new
640+
# string dtype of Pandas <= 3. See
641+
# https://pandas.pydata.org/pandas-docs/stable/whatsnew/v3.0.0.html#dedicated-string-data-type-by-default
642+
# for details.
643+
if pandas_object.index.dtype == "str":
644+
pandas_object.index = pandas_object.index.astype("object")
645+
if (
646+
hasattr(pandas_object, "columns")
647+
and pandas_object.columns.dtype == "str"
648+
):
649+
pandas_object.columns = pandas_object.columns.astype("object")
638650
cube = iris.pandas.as_cube(pandas_object)
639651
for coord_name in ("index", "columns"):
640652
try:

0 commit comments

Comments
 (0)