Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates for pandas v2.2 and xarray v2024.02 #404

Merged
merged 2 commits into from
Feb 27, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Internal Changes

- Start testing the minimum versions of required dependencies (`#398 <https://github.com/MESMER-group/mesmer/pull/398>`_).
By `Mathias Hauser`_.
- Restore compatibility with pandas version v2.2 and xarray version v2024.02 (`#404 <https://github.com/MESMER-group/mesmer/pull/404>`_).
By `Mathias Hauser`_.


v0.10.0 - 2024.01.04
Expand Down
15 changes: 9 additions & 6 deletions mesmer/core/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pandas as pd
import pooch
import xarray as xr
from packaging.version import Version

import mesmer

Expand Down Expand Up @@ -36,18 +38,19 @@ def _load_aod_obs(*, version, resample):

df = pd.read_csv(
filename,
delim_whitespace=True,
sep=r"\s+",
skiprows=11,
names=("year", "month", "aod"),
parse_dates=[["year", "month"]],
date_format={"year_month": "%Y %m"},
index_col="year_month",
dtype={"year": str, "month": str},
)

aod = df.to_xarray().rename(year_month="time").aod
time = pd.to_datetime(df.year + df.month, format="%Y%m")

aod = xr.DataArray(df.aod.values, coords={"time": time}, name="aod")

if resample:
aod = aod.resample(time="A").mean()
freq = "YE" if Version(xr.__version__) >= Version("2024.02") else "A"
aod = aod.resample(time=freq).mean()

return aod

Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pandas as pd
import xarray as xr
from packaging.version import Version

from mesmer.core._data import load_stratospheric_aerosol_optical_depth_obs

Expand All @@ -9,7 +10,9 @@ def test_load_stratospheric_aerosol_optical_depth_data():

aod = load_stratospheric_aerosol_optical_depth_obs(version="2022", resample=True)

time = pd.date_range("1850", "2023", freq="A")
freq = "YE" if Version(pd.__version__) >= Version("2.2") else "A"

time = pd.date_range("1850", "2023", freq=freq)
time = xr.DataArray(time, dims="time", coords={"time": time})

xr.testing.assert_equal(aod.time, time)
Expand Down
29 changes: 7 additions & 22 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pandas as pd
import pytest
import xarray as xr
from packaging.version import Version
Expand Down Expand Up @@ -196,29 +195,15 @@ def test_check_dataarray_form_shape():


def _get_time(*args, **kwargs):
# TODO: use xr.date_range once requiring xarray >= v0.21

calendar = kwargs.pop("calendar", "standard")
freq = kwargs.pop("freq", None)

# translate freq strings
pandas_calendars = ["standard", "gregorian"]
if freq:
if calendar not in pandas_calendars and Version(xr.__version__) < Version(
"2023.11"
):
freq = freq.replace("Y", "A").replace("ME", "M")
if calendar in pandas_calendars and Version(pd.__version__) < Version("2.2"):
freq = freq.replace("Y", "A").replace("ME", "M")

if Version(xr.__version__) >= Version("0.21"):
time = xr.date_range(*args, calendar=calendar, freq=freq, **kwargs)
else:

if calendar == "standard":
time = pd.date_range(*args, freq=freq, **kwargs)
else:
time = xr.cftime_range(*args, calendar=calendar, freq=freq, **kwargs)
if freq and Version(xr.__version__) < Version("2024.02"):
freq = freq.replace("YE", "A").replace("ME", "M")

time = xr.date_range(*args, calendar=calendar, freq=freq, **kwargs)

return xr.DataArray(time, dims="time")

Expand All @@ -228,14 +213,14 @@ def _get_time(*args, **kwargs):
)
def test_assert_annual_data(calendar):

time = _get_time("2000", "2005", freq="Y", calendar=calendar)
time = _get_time("2000", "2005", freq="YE", calendar=calendar)

# no error
mesmer.core.utils._assert_annual_data(time)


@pytest.mark.parametrize("calendar", ["standard", "gregorian", "365_day"])
@pytest.mark.parametrize("freq", ["2Y", "ME"])
@pytest.mark.parametrize("freq", ["2YE", "ME"])
def test_assert_annual_data_wrong_freq(calendar, freq):

time = _get_time("2000", periods=5, freq=freq, calendar=calendar)
Expand All @@ -248,7 +233,7 @@ def test_assert_annual_data_wrong_freq(calendar, freq):

def test_assert_annual_data_unkown_freq():

time1 = _get_time("2000", periods=2, freq="Y")
time1 = _get_time("2000", periods=2, freq="YE")
time2 = _get_time("2002", periods=3, freq="ME")
time = xr.concat([time1, time2], dim="time")

Expand Down