From ed23adc6a8bb6f35dcde79b5c7743d48133dc81a Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sun, 9 Jun 2024 13:28:20 -0400 Subject: [PATCH] Address more warnings --- xarray/tests/test_backends.py | 2 +- xarray/tests/test_dataarray.py | 8 ++++++-- xarray/tests/test_groupby.py | 7 +++---- xarray/tests/test_plot.py | 7 +++---- xarray/tests/test_variable.py | 8 +++++--- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 33039dee7b0..177700a5404 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -529,7 +529,7 @@ def test_roundtrip_string_encoded_characters(self) -> None: assert actual["x"].encoding["_Encoding"] == "ascii" def test_roundtrip_numpy_datetime_data(self) -> None: - times = pd.to_datetime(["2000-01-01", "2000-01-02", "NaT"]) + times = pd.to_datetime(["2000-01-01", "2000-01-02", "NaT"], unit="ns") expected = Dataset({"t": ("t", times), "t0": times[0]}) kwargs = {"encoding": {"t0": {"units": "days since 1950-01-01"}}} with self.roundtrip(expected, save_kwargs=kwargs) as actual: diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 4e916d62155..45c7dc80a30 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -3647,7 +3647,9 @@ def test_to_and_from_dict_with_time_dim(self) -> None: t = pd.date_range("20130101", periods=10) lat = [77.7, 83.2, 76] da = DataArray(x, {"t": t, "lat": lat}, dims=["t", "lat"]) - roundtripped = DataArray.from_dict(da.to_dict()) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message="Converting non-nanosecond") + roundtripped = DataArray.from_dict(da.to_dict()) assert_identical(da, roundtripped) def test_to_and_from_dict_with_nan_nat(self) -> None: @@ -3657,7 +3659,9 @@ def test_to_and_from_dict_with_nan_nat(self) -> None: t[2] = np.nan lat = [77.7, 83.2, 76] da = DataArray(y, {"t": t, "lat": lat}, dims=["t", "lat"]) - roundtripped = DataArray.from_dict(da.to_dict()) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message="Converting non-nanosecond") + roundtripped = DataArray.from_dict(da.to_dict()) assert_identical(da, roundtripped) def test_to_dict_with_numpy_attrs(self) -> None: diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 7134fe96d01..1547181467a 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -139,8 +139,7 @@ def test_groupby_da_datetime() -> None: times = pd.date_range("2000-01-01", periods=4) foo = xr.DataArray([1, 2, 3, 4], coords=dict(time=times), dims="time") # create test index - dd = times.to_pydatetime() - reference_dates = [dd[0], dd[2]] + reference_dates = [times[0], times[2]] labels = reference_dates[0:1] * 2 + reference_dates[1:2] * 2 ind = xr.DataArray( labels, coords=dict(time=times), dims="time", name="reference_date" @@ -566,7 +565,7 @@ def test_da_groupby_assign_coords() -> None: coords={ "z": ["a", "b", "c", "a", "b", "c"], "x": [1, 1, 1, 2, 2, 3, 4, 5, 3, 4], - "t": xr.date_range("2001-01-01", freq="ME", periods=24, use_cftime=False), + "t": xr.date_range("2001-01-01", freq="MS", periods=24, use_cftime=False), "month": ("t", list(range(1, 13)) * 2), }, ) @@ -1881,7 +1880,7 @@ def test_resample_first(self) -> None: array = Dataset({"time": times})["time"] actual = array.resample(time="1D").last() expected_times = pd.to_datetime( - ["2000-01-01T18", "2000-01-02T18", "2000-01-03T06"] + ["2000-01-01T18", "2000-01-02T18", "2000-01-03T06"], unit="ns" ) expected = DataArray(expected_times, [("time", times[::4])], name="time") assert_identical(expected, actual) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 27f4ded5646..21dc12d6f38 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -5,7 +5,7 @@ import math from collections.abc import Generator, Hashable from copy import copy -from datetime import date, datetime, timedelta +from datetime import date, timedelta from typing import Any, Callable, Literal import numpy as np @@ -2912,9 +2912,8 @@ def setUp(self) -> None: """ month = np.arange(1, 13, 1) data = np.sin(2 * np.pi * month / 12.0) - - darray = DataArray(data, dims=["time"]) - darray.coords["time"] = np.array([datetime(2017, m, 1) for m in month]) + times = pd.date_range(start="2017-01-01", freq="ME", periods=12) + darray = DataArray(data, dims=["time"], coords=[times]) self.darray = darray diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 3167de2e2f0..6097971cc91 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -256,7 +256,9 @@ def test_index_and_concat_datetime(self): # regression test for #125 date_range = pd.date_range("2011-09-01", periods=10) for dates in [date_range, date_range.values, date_range.to_pydatetime()]: - expected = self.cls("t", dates) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message="Converting non-nanosecond") + expected = self.cls("t", dates) for times in [ [expected[i] for i in range(10)], [expected[i : (i + 1)] for i in range(10)], @@ -2942,8 +2944,8 @@ def test_from_pint_wrapping_dask(self, Var): (np.array([np.datetime64("2000-01-01", "ns")]), False), (np.array([np.datetime64("2000-01-01", "s")]), True), (pd.date_range("2000", periods=1), False), - (datetime(2000, 1, 1), False), - (np.array([datetime(2000, 1, 1)]), False), + (datetime(2000, 1, 1), True), + (np.array([datetime(2000, 1, 1)]), True), (pd.date_range("2000", periods=1, tz=pytz.timezone("America/New_York")), False), ( pd.Series(