Skip to content

Commit 05c4bf5

Browse files
spencerkclarkandersy005
authored andcommitted
Address latest pandas-related upstream test failures (#9081)
* Address pandas-related upstream test failures * Address more warnings * Don't lose coverage for pandas < 3 * Address one more warning * Fix accidental change from MS to ME * Use datetime64[ns] arrays * Switch to @pytest.mark.filterwarnings
1 parent 65d27fc commit 05c4bf5

8 files changed

+33
-17
lines changed

xarray/coding/cftime_offsets.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,21 @@
7777
from xarray.core.types import InclusiveOptions, SideOptions
7878

7979

80+
def _nanosecond_precision_timestamp(*args, **kwargs):
81+
# As of pandas version 3.0, pd.to_datetime(Timestamp(...)) will try to
82+
# infer the appropriate datetime precision. Until xarray supports
83+
# non-nanosecond precision times, we will use this constructor wrapper to
84+
# explicitly create nanosecond-precision Timestamp objects.
85+
return pd.Timestamp(*args, **kwargs).as_unit("ns")
86+
87+
8088
def get_date_type(calendar, use_cftime=True):
8189
"""Return the cftime date type for a given calendar name."""
8290
if cftime is None:
8391
raise ImportError("cftime is required for dates with non-standard calendars")
8492
else:
8593
if _is_standard_calendar(calendar) and not use_cftime:
86-
return pd.Timestamp
94+
return _nanosecond_precision_timestamp
8795

8896
calendars = {
8997
"noleap": cftime.DatetimeNoLeap,

xarray/tests/test_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def test_roundtrip_string_encoded_characters(self) -> None:
529529
assert actual["x"].encoding["_Encoding"] == "ascii"
530530

531531
def test_roundtrip_numpy_datetime_data(self) -> None:
532-
times = pd.to_datetime(["2000-01-01", "2000-01-02", "NaT"])
532+
times = pd.to_datetime(["2000-01-01", "2000-01-02", "NaT"], unit="ns")
533533
expected = Dataset({"t": ("t", times), "t0": times[0]})
534534
kwargs = {"encoding": {"t0": {"units": "days since 1950-01-01"}}}
535535
with self.roundtrip(expected, save_kwargs=kwargs) as actual:

xarray/tests/test_coding_times.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,14 @@ def test_infer_datetime_units(freq, units) -> None:
538538
["dates", "expected"],
539539
[
540540
(
541-
pd.to_datetime(["1900-01-01", "1900-01-02", "NaT"]),
541+
pd.to_datetime(["1900-01-01", "1900-01-02", "NaT"], unit="ns"),
542542
"days since 1900-01-01 00:00:00",
543543
),
544-
(pd.to_datetime(["NaT", "1900-01-01"]), "days since 1900-01-01 00:00:00"),
545-
(pd.to_datetime(["NaT"]), "days since 1970-01-01 00:00:00"),
544+
(
545+
pd.to_datetime(["NaT", "1900-01-01"], unit="ns"),
546+
"days since 1900-01-01 00:00:00",
547+
),
548+
(pd.to_datetime(["NaT"], unit="ns"), "days since 1970-01-01 00:00:00"),
546549
],
547550
)
548551
def test_infer_datetime_units_with_NaT(dates, expected) -> None:

xarray/tests/test_combine.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from datetime import datetime
43
from itertools import product
54

65
import numpy as np
@@ -229,8 +228,12 @@ def test_lexicographic_sort_string_coords(self):
229228
assert concat_dims == ["simulation"]
230229

231230
def test_datetime_coords(self):
232-
ds0 = Dataset({"time": [datetime(2000, 3, 6), datetime(2001, 3, 7)]})
233-
ds1 = Dataset({"time": [datetime(1999, 1, 1), datetime(1999, 2, 4)]})
231+
ds0 = Dataset(
232+
{"time": np.array(["2000-03-06", "2000-03-07"], dtype="datetime64[ns]")}
233+
)
234+
ds1 = Dataset(
235+
{"time": np.array(["1999-01-01", "1999-02-04"], dtype="datetime64[ns]")}
236+
)
234237

235238
expected = {(0,): ds1, (1,): ds0}
236239
actual, concat_dims = _infer_concat_order_from_coords([ds0, ds1])

xarray/tests/test_dataarray.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,7 @@ def test_to_and_from_dict(
36423642
actual_no_data = da.to_dict(data=False, encoding=encoding)
36433643
assert expected_no_data == actual_no_data
36443644

3645+
@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
36453646
def test_to_and_from_dict_with_time_dim(self) -> None:
36463647
x = np.random.randn(10, 3)
36473648
t = pd.date_range("20130101", periods=10)
@@ -3650,6 +3651,7 @@ def test_to_and_from_dict_with_time_dim(self) -> None:
36503651
roundtripped = DataArray.from_dict(da.to_dict())
36513652
assert_identical(da, roundtripped)
36523653

3654+
@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
36533655
def test_to_and_from_dict_with_nan_nat(self) -> None:
36543656
y = np.random.randn(10, 3)
36553657
y[2] = np.nan

xarray/tests/test_groupby.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ def test_groupby_da_datetime() -> None:
139139
times = pd.date_range("2000-01-01", periods=4)
140140
foo = xr.DataArray([1, 2, 3, 4], coords=dict(time=times), dims="time")
141141
# create test index
142-
dd = times.to_pydatetime()
143-
reference_dates = [dd[0], dd[2]]
142+
reference_dates = [times[0], times[2]]
144143
labels = reference_dates[0:1] * 2 + reference_dates[1:2] * 2
145144
ind = xr.DataArray(
146145
labels, coords=dict(time=times), dims="time", name="reference_date"
@@ -1881,7 +1880,7 @@ def test_resample_first(self) -> None:
18811880
array = Dataset({"time": times})["time"]
18821881
actual = array.resample(time="1D").last()
18831882
expected_times = pd.to_datetime(
1884-
["2000-01-01T18", "2000-01-02T18", "2000-01-03T06"]
1883+
["2000-01-01T18", "2000-01-02T18", "2000-01-03T06"], unit="ns"
18851884
)
18861885
expected = DataArray(expected_times, [("time", times[::4])], name="time")
18871886
assert_identical(expected, actual)

xarray/tests/test_plot.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import math
66
from collections.abc import Generator, Hashable
77
from copy import copy
8-
from datetime import date, datetime, timedelta
8+
from datetime import date, timedelta
99
from typing import Any, Callable, Literal
1010

1111
import numpy as np
@@ -2912,9 +2912,8 @@ def setUp(self) -> None:
29122912
"""
29132913
month = np.arange(1, 13, 1)
29142914
data = np.sin(2 * np.pi * month / 12.0)
2915-
2916-
darray = DataArray(data, dims=["time"])
2917-
darray.coords["time"] = np.array([datetime(2017, m, 1) for m in month])
2915+
times = pd.date_range(start="2017-01-01", freq="MS", periods=12)
2916+
darray = DataArray(data, dims=["time"], coords=[times])
29182917

29192918
self.darray = darray
29202919

xarray/tests/test_variable.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
assert_equal,
3737
assert_identical,
3838
assert_no_warnings,
39+
has_pandas_3,
3940
raise_if_dask_computes,
4041
requires_bottleneck,
4142
requires_cupy,
@@ -252,6 +253,7 @@ def test_0d_object_array_with_list(self):
252253
assert_array_equal(x[0].data, listarray.squeeze())
253254
assert_array_equal(x.squeeze().data, listarray.squeeze())
254255

256+
@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
255257
def test_index_and_concat_datetime(self):
256258
# regression test for #125
257259
date_range = pd.date_range("2011-09-01", periods=10)
@@ -2942,8 +2944,8 @@ def test_from_pint_wrapping_dask(self, Var):
29422944
(np.array([np.datetime64("2000-01-01", "ns")]), False),
29432945
(np.array([np.datetime64("2000-01-01", "s")]), True),
29442946
(pd.date_range("2000", periods=1), False),
2945-
(datetime(2000, 1, 1), False),
2946-
(np.array([datetime(2000, 1, 1)]), False),
2947+
(datetime(2000, 1, 1), has_pandas_3),
2948+
(np.array([datetime(2000, 1, 1)]), has_pandas_3),
29472949
(pd.date_range("2000", periods=1, tz=pytz.timezone("America/New_York")), False),
29482950
(
29492951
pd.Series(

0 commit comments

Comments
 (0)