From df296499ef59efcc5a02fee112b1e512d023279e Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 25 Jun 2024 16:07:15 +0200 Subject: [PATCH 1/2] use a `composite` to generate the dataframe with a tz-aware dt column --- properties/test_pandas_roundtrip.py | 34 +++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/properties/test_pandas_roundtrip.py b/properties/test_pandas_roundtrip.py index 0249aa59d5b..a54ccfe49f6 100644 --- a/properties/test_pandas_roundtrip.py +++ b/properties/test_pandas_roundtrip.py @@ -25,22 +25,34 @@ numeric_series = numeric_dtypes.flatmap(lambda dt: pdst.series(dtype=dt)) + +@st.composite +def dataframe_strategy(draw): + tz = draw(st.timezones()) + dtype = pd.DatetimeTZDtype(unit="ns", tz=tz) + + datetimes = st.datetimes( + min_value=pd.Timestamp("1677-09-21T00:12:43.145224193"), + max_value=pd.Timestamp("2262-04-11T23:47:16.854775807"), + timezones=st.just(tz), + ) + + df = pdst.data_frames( + [ + pdst.column("datetime_col", elements=datetimes), + pdst.column("other_col", elements=st.integers()), + ], + index=pdst.range_indexes(min_size=1, max_size=10), + ) + return draw(df).astype({"datetime_col": dtype}) + + an_array = npst.arrays( dtype=numeric_dtypes, shape=npst.array_shapes(max_dims=2), # can only convert 1D/2D to pandas ) -datetime_with_tz_strategy = st.datetimes(timezones=st.timezones()) -dataframe_strategy = pdst.data_frames( - [ - pdst.column("datetime_col", elements=datetime_with_tz_strategy), - pdst.column("other_col", elements=st.integers()), - ], - index=pdst.range_indexes(min_size=1, max_size=10), -) - - @st.composite def datasets_1d_vars(draw) -> xr.Dataset: """Generate datasets with only 1D variables @@ -115,7 +127,7 @@ def test_roundtrip_pandas_dataframe(df) -> None: has_pandas_3, reason="fails to roundtrip on pandas 3 (see https://github.com/pydata/xarray/issues/9098)", ) -@given(df=dataframe_strategy) +@given(df=dataframe_strategy()) def test_roundtrip_pandas_dataframe_datetime(df) -> None: # Need to name the indexes, otherwise Xarray names them 'dim_0', 'dim_1'. df.index.name = "rows" From 1532cef49c48b541cc30e7cae2c8840f0d1ff142 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 25 Jun 2024 16:16:38 +0200 Subject: [PATCH 2/2] remove the `xfail` --- properties/test_pandas_roundtrip.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/properties/test_pandas_roundtrip.py b/properties/test_pandas_roundtrip.py index a54ccfe49f6..9e0d4640171 100644 --- a/properties/test_pandas_roundtrip.py +++ b/properties/test_pandas_roundtrip.py @@ -9,7 +9,6 @@ import pytest import xarray as xr -from xarray.tests import has_pandas_3 pytest.importorskip("hypothesis") import hypothesis.extra.numpy as npst # isort:skip @@ -123,10 +122,6 @@ def test_roundtrip_pandas_dataframe(df) -> None: xr.testing.assert_identical(arr, roundtripped.to_xarray()) -@pytest.mark.skipif( - has_pandas_3, - reason="fails to roundtrip on pandas 3 (see https://github.com/pydata/xarray/issues/9098)", -) @given(df=dataframe_strategy()) def test_roundtrip_pandas_dataframe_datetime(df) -> None: # Need to name the indexes, otherwise Xarray names them 'dim_0', 'dim_1'.