Skip to content

Commit

Permalink
Merge branch 'main' into drop-python3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian authored Aug 13, 2024
2 parents 7872c7e + ce5130f commit d29b8c9
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 46 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
types: [opened, reopened, synchronize, labeled]
workflow_dispatch:

env:
PR_HEAD_LABEL: ${{ github.event.pull_request.head.label }}

jobs:
benchmark:
if: ${{ contains( github.event.pull_request.labels.*.name, 'run-benchmark') && github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }}
Expand Down Expand Up @@ -49,7 +52,7 @@ jobs:
# ID this runner
asv machine --yes
echo "Baseline: ${{ github.event.pull_request.base.sha }} (${{ github.event.pull_request.base.label }})"
echo "Contender: ${GITHUB_SHA} (${{ github.event.pull_request.head.label }})"
echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)"
# Run benchmarks for current commit against base
ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR"
asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \
Expand Down
4 changes: 2 additions & 2 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
SerializationWarning,
stacklevel=10,
)
data = np.around(data)
data = data.astype(dtype=dtype)
data = duck_array_ops.round(data)
data = duck_array_ops.astype(data, dtype=dtype)
return Variable(dims, data, attrs, encoding, fastpath=True)
else:
return variable
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7228,7 +7228,7 @@ def coarsen(
User guide describing :py:func:`~xarray.DataArray.coarsen`
:ref:`compute.coarsen`
User guide on block arrgragation :py:func:`~xarray.DataArray.coarsen`
User guide on block aggregation :py:func:`~xarray.DataArray.coarsen`
:doc:`xarray-tutorial:fundamentals/03.3_windowed`
Tutorial on windowed computation using :py:func:`~xarray.DataArray.coarsen`
Expand Down
42 changes: 9 additions & 33 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import warnings
from functools import partial
from importlib import import_module
from typing import Callable

import numpy as np
import pandas as pd
from numpy import all as array_all # noqa
from numpy import any as array_any # noqa
from numpy import concatenate as _concatenate
from numpy import ( # noqa
around, # noqa
full_like,
gradient,
isclose,
Expand All @@ -29,7 +30,6 @@
transpose,
unravel_index,
)
from numpy import concatenate as _concatenate
from numpy.lib.stride_tricks import sliding_window_view # noqa
from packaging.version import Version
from pandas.api.types import is_extension_array_dtype
Expand Down Expand Up @@ -122,37 +122,13 @@ def fail_on_dask_array_input(values, msg=None, func_name=None):
# Requires special-casing because pandas won't automatically dispatch to dask.isnull via NEP-18
pandas_isnull = _dask_or_eager_func("isnull", eager_module=pd, dask_module="dask.array")

# np.around has failing doctests, overwrite it so they pass:
# https://github.com/numpy/numpy/issues/19759
around.__doc__ = str.replace(
around.__doc__ or "",
"array([0., 2.])",
"array([0., 2.])",
)
around.__doc__ = str.replace(
around.__doc__ or "",
"array([0., 2.])",
"array([0., 2.])",
)
around.__doc__ = str.replace(
around.__doc__ or "",
"array([0.4, 1.6])",
"array([0.4, 1.6])",
)
around.__doc__ = str.replace(
around.__doc__ or "",
"array([0., 2., 2., 4., 4.])",
"array([0., 2., 2., 4., 4.])",
)
around.__doc__ = str.replace(
around.__doc__ or "",
(
' .. [2] "How Futile are Mindless Assessments of\n'
' Roundoff in Floating-Point Computation?", William Kahan,\n'
" https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf\n"
),
"",
)

def round(array):
xp = get_array_namespace(array)
return xp.round(array)


around: Callable = round


def isnull(data):
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def test_roundtrip_cftime_datetime_data(self) -> None:
assert actual.t.encoding["calendar"] == expected_calendar

def test_roundtrip_timedelta_data(self) -> None:
time_deltas = pd.to_timedelta(["1h", "2h", "NaT"]) # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
time_deltas = pd.to_timedelta(["1h", "2h", "NaT"])
expected = Dataset({"td": ("td", time_deltas), "td0": time_deltas[0]})
with self.roundtrip(expected) as actual:
assert_identical(expected, actual)
Expand Down
8 changes: 4 additions & 4 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,10 @@ def test_cf_timedelta_2d() -> None:
@pytest.mark.parametrize(
["deltas", "expected"],
[
(pd.to_timedelta(["1 day", "2 days"]), "days"), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
(pd.to_timedelta(["1h", "1 day 1 hour"]), "hours"), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
(pd.to_timedelta(["1m", "2m", np.nan]), "minutes"), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
(pd.to_timedelta(["1m3s", "1m4s"]), "seconds"), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
(pd.to_timedelta(["1 day", "2 days"]), "days"),
(pd.to_timedelta(["1h", "1 day 1 hour"]), "hours"),
(pd.to_timedelta(["1m", "2m", np.nan]), "minutes"),
(pd.to_timedelta(["1m3s", "1m4s"]), "seconds"),
],
)
def test_infer_timedelta_units(deltas, expected) -> None:
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_incompatible_attributes(self) -> None:
Variable(
["t"], pd.date_range("2000-01-01", periods=3), {"units": "foobar"}
),
Variable(["t"], pd.to_timedelta(["1 day"]), {"units": "foobar"}), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
Variable(["t"], pd.to_timedelta(["1 day"]), {"units": "foobar"}),
Variable(["t"], [0, 1, 2], {"add_offset": 0}, {"add_offset": 2}),
Variable(["t"], [0, 1, 2], {"_FillValue": 0}, {"_FillValue": 2}),
]
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def test_format_items(self) -> None:
np.arange(4) * np.timedelta64(500, "ms"),
"00:00:00 00:00:00.500000 00:00:01 00:00:01.500000",
),
(pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
(pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"),
(
pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]), # type: ignore[arg-type] #https://github.com/pandas-dev/pandas-stubs/issues/956
pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]),
"1 days 01:00:00 1 days 00:00:00 0 days 00:00:00",
),
([1, 2, 3], "1 2 3"),
Expand Down
25 changes: 24 additions & 1 deletion xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import copy
import sys
from abc import abstractmethod
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, Generic, cast, overload
Expand Down Expand Up @@ -86,7 +87,29 @@ def check_duck_array_typevar(a: duckarray[Any, _DType]) -> duckarray[Any, _DType
if isinstance(b, _arrayfunction_or_api):
return b
else:
raise TypeError(f"a ({type(a)}) is not a valid _arrayfunction or _arrayapi")
missing_attrs = ""
actual_attrs = set(dir(b))
for t in _arrayfunction_or_api:
if sys.version_info >= (3, 13):
# https://github.com/python/cpython/issues/104873
from typing import get_protocol_members

expected_attrs = get_protocol_members(t)
elif sys.version_info >= (3, 12):
expected_attrs = t.__protocol_attrs__
else:
from typing import _get_protocol_attrs # type: ignore[attr-defined]

expected_attrs = _get_protocol_attrs(t)

missing_attrs_ = expected_attrs - actual_attrs
if missing_attrs_:
missing_attrs += f"{t.__name__} - {missing_attrs_}\n"
raise TypeError(
f"a ({type(a)}) is not a valid _arrayfunction or _arrayapi. "
"Missing following attrs:\n"
f"{missing_attrs}"
)


class NamedArraySubclassobjects:
Expand Down

0 comments on commit d29b8c9

Please sign in to comment.