Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/983.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bug fix for pandas >= 3 migration
37 changes: 37 additions & 0 deletions src/sisl/viz/plotters/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,48 @@
from typing import Literal, Optional, Union

import numpy as np
import pandas as pd
from xarray import DataArray, Dataset

from . import plot_actions

# from sisl.viz.nodes.processors.grid import get_isos


def _correct_pandas_string_none(dic: dict[str, DataArray]):
"""Correct string arguments in xarray arrays to avoid casting to nan

This is a migration notice from pandas >= 3.
They change their str implementation to a custom (new default) dtype.
And the result is that `None` will be parsed to `np.nan` as opposed
to the old `None -> None` parsing.
"""

# xarray uses pandas for casting types, and pandas decided to change
# the default behavior and parse [None, ""], to [nan, ""]. Passing
# NaN to plotting functions when they are expecting None breaks things.
# Here we make sure that we parse all NaN to None
# Pandas docs on this:
# https://pandas.pydata.org/docs/user_guide/migration-3-strings.html
old_infer_string = pd.options.future.infer_string
pd.options.future.infer_string = False

for key, value in dic.items():
value = dic[key]
try:
dic[key] = value.where(value.notnull(), other=None)
except:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.

Check notice

Code scanning / CodeQL

Except block handles 'BaseException' Note

Except block directly handles BaseException.
pass

pd.options.future.infer_string = old_infer_string


if int(pd.__version__.split(".")[0]) >= 3:

def _correct_pandas_string_none(dic: dict[str, str]):
pass


def _process_xarray_data(
data: Union[DataArray, Dataset],
x: Union[str, None] = None,
Expand Down Expand Up @@ -461,6 +496,8 @@
"border_width",
"border_color",
)
_correct_pandas_string_none(style)

for key in style_keys:
lines_style[key] = style.get(key)

Expand Down
Loading