Skip to content

Commit 6c2d8c3

Browse files
authored
use a composite strategy to generate the dataframe with a tz-aware datetime column (#9174)
* use a `composite` to generate the dataframe with a tz-aware dt column * remove the `xfail`
1 parent 90e4486 commit 6c2d8c3

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

properties/test_pandas_roundtrip.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pytest
1010

1111
import xarray as xr
12-
from xarray.tests import has_pandas_3
1312

1413
pytest.importorskip("hypothesis")
1514
import hypothesis.extra.numpy as npst # isort:skip
@@ -25,22 +24,34 @@
2524

2625
numeric_series = numeric_dtypes.flatmap(lambda dt: pdst.series(dtype=dt))
2726

27+
28+
@st.composite
29+
def dataframe_strategy(draw):
30+
tz = draw(st.timezones())
31+
dtype = pd.DatetimeTZDtype(unit="ns", tz=tz)
32+
33+
datetimes = st.datetimes(
34+
min_value=pd.Timestamp("1677-09-21T00:12:43.145224193"),
35+
max_value=pd.Timestamp("2262-04-11T23:47:16.854775807"),
36+
timezones=st.just(tz),
37+
)
38+
39+
df = pdst.data_frames(
40+
[
41+
pdst.column("datetime_col", elements=datetimes),
42+
pdst.column("other_col", elements=st.integers()),
43+
],
44+
index=pdst.range_indexes(min_size=1, max_size=10),
45+
)
46+
return draw(df).astype({"datetime_col": dtype})
47+
48+
2849
an_array = npst.arrays(
2950
dtype=numeric_dtypes,
3051
shape=npst.array_shapes(max_dims=2), # can only convert 1D/2D to pandas
3152
)
3253

3354

34-
datetime_with_tz_strategy = st.datetimes(timezones=st.timezones())
35-
dataframe_strategy = pdst.data_frames(
36-
[
37-
pdst.column("datetime_col", elements=datetime_with_tz_strategy),
38-
pdst.column("other_col", elements=st.integers()),
39-
],
40-
index=pdst.range_indexes(min_size=1, max_size=10),
41-
)
42-
43-
4455
@st.composite
4556
def datasets_1d_vars(draw) -> xr.Dataset:
4657
"""Generate datasets with only 1D variables
@@ -111,11 +122,7 @@ def test_roundtrip_pandas_dataframe(df) -> None:
111122
xr.testing.assert_identical(arr, roundtripped.to_xarray())
112123

113124

114-
@pytest.mark.skipif(
115-
has_pandas_3,
116-
reason="fails to roundtrip on pandas 3 (see https://github.com/pydata/xarray/issues/9098)",
117-
)
118-
@given(df=dataframe_strategy)
125+
@given(df=dataframe_strategy())
119126
def test_roundtrip_pandas_dataframe_datetime(df) -> None:
120127
# Need to name the indexes, otherwise Xarray names them 'dim_0', 'dim_1'.
121128
df.index.name = "rows"

0 commit comments

Comments
 (0)