1212import pandas as pd
1313import pytest
1414import scipy
15+ import xarray as xr
1516from boltons .iterutils import default_exit , remap , research
1617from numpy import ma
1718from packaging .version import Version
@@ -146,6 +147,14 @@ def fix_known_differences(
146147 orig = orig .copy ()
147148 result = result .copy ()
148149
150+ if backwards_compat :
151+ del orig .varm
152+ del orig .varp
153+ if isinstance (result .obs , XDataset ):
154+ result .obs = result .obs .drop_vars (["batch" ])
155+ else :
156+ result .obs .drop (columns = ["batch" ], inplace = True )
157+
149158 for attrname in ("obs" , "var" ):
150159 if isinstance (getattr (result , attrname ), XDataset ):
151160 for adata in (orig , result ):
@@ -171,11 +180,6 @@ def fix_known_differences(
171180 # * merge obsp, but some information should be lost
172181 del orig .obsp # TODO
173182
174- if backwards_compat :
175- del orig .varm
176- del orig .varp
177- result .obs .drop (columns = ["batch" ], inplace = True )
178-
179183 # Possibly need to fix this, ordered categoricals lose orderedness
180184 for get_df in [lambda k : k .obs , lambda k : k .obsm ["df" ]]:
181185 str_to_df_converted = get_df (result )
@@ -234,9 +238,6 @@ def test_concatenate_roundtrip(
234238 ** GEN_ADATA_DASK_ARGS ,
235239 )
236240
237- if backwards_compat and (obs_xdataset or var_xdataset ):
238- pytest .xfail ("https://github.com/pydata/xarray/issues/10218" )
239-
240241 remaining = adata .obs_names
241242 subsets = []
242243 while len (remaining ) > 0 :
@@ -246,6 +247,16 @@ def test_concatenate_roundtrip(
246247 remaining = remaining .difference (subset_idx )
247248
248249 result = concat_func (subsets , join = join_type , uns_merge = "same" , index_unique = None )
250+ if (
251+ backwards_compat
252+ and (obs_xdataset or var_xdataset )
253+ and Version (xr .__version__ ) < Version ("2025.4.0" )
254+ ):
255+ pytest .xfail ("https://github.com/pydata/xarray/issues/10218" )
256+ if backwards_compat and var_xdataset :
257+ result .var = xr .Dataset .from_dataframe (
258+ result .var
259+ ) # backwards compat always returns a dataframe
249260
250261 # Correcting for known differences
251262 orig , result = fix_known_differences (
0 commit comments