From 675b60bfe507caaad715bb8f3ff322289278ad98 Mon Sep 17 00:00:00 2001 From: matthmey Date: Mon, 11 Nov 2019 16:41:07 +0100 Subject: [PATCH 1/8] added strides to rolling --- xarray/core/common.py | 7 ++++++- xarray/core/rolling.py | 28 +++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index d372115ea57..83fb66fbccd 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -743,6 +743,7 @@ def rolling( dim: Mapping[Hashable, int] = None, min_periods: int = None, center: bool = False, + stride: int = 1, **window_kwargs: int, ): """ @@ -759,6 +760,8 @@ def rolling( setting min_periods equal to the size of the window. center : boolean, default False Set the labels at the center of the window. + stride : int, default 1 + Stride of the moving window **window_kwargs : optional The keyword arguments form of ``dim``. One of dim or window_kwargs must be provided. @@ -801,7 +804,9 @@ def rolling( core.rolling.DatasetRolling """ dim = either_dict_or_kwargs(dim, window_kwargs, "rolling") - return self._rolling_cls(self, dim, min_periods=min_periods, center=center) + return self._rolling_cls( + self, dim, min_periods=min_periods, center=center, stride=stride + ) def rolling_exp( self, diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index f4e571a8efe..b8793f7e6ea 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -140,9 +140,9 @@ def count(self): class DataArrayRolling(Rolling): - __slots__ = ("window_labels",) + __slots__ = ("window_labels", "stride") - def __init__(self, obj, windows, min_periods=None, center=False): + def __init__(self, obj, windows, min_periods=None, center=False, stride=1): """ Moving window object for DataArray. You should use DataArray.rolling() method to construct this object @@ -164,6 +164,8 @@ def __init__(self, obj, windows, min_periods=None, center=False): setting min_periods equal to the size of the window. center : boolean, default False Set the labels at the center of the window. + stride : int, default 1 + Stride of the moving window Returns ------- @@ -179,12 +181,19 @@ def __init__(self, obj, windows, min_periods=None, center=False): super().__init__(obj, windows, min_periods=min_periods, center=center) self.window_labels = self.obj[self.dim] + self.stride = stride def __iter__(self): stops = np.arange(1, len(self.window_labels) + 1) starts = stops - int(self.window) starts[: int(self.window)] = 0 - for (label, start, stop) in zip(self.window_labels, starts, stops): + + # apply striding + stops = stops[:: self.stride] + starts = starts[:: self.stride] + window_labels = self.window_labels[:: self.stride] + + for (label, start, stop) in zip(window_labels, starts, stops): window = self.obj.isel(**{self.dim: slice(start, stop)}) counts = window.count(dim=self.dim) @@ -282,7 +291,7 @@ def reduce(self, func, **kwargs): [ 4., 9., 15., 18.]]) """ rolling_dim = utils.get_temp_dimname(self.obj.dims, "_rolling_dim") - windows = self.construct(rolling_dim) + windows = self.construct(rolling_dim, stride=self.stride) result = windows.reduce(func, dim=rolling_dim, **kwargs) # Find valid windows based on count. @@ -300,11 +309,12 @@ def _counts(self): counts = ( self.obj.notnull() .rolling(center=self.center, **{self.dim: self.window}) - .construct(rolling_dim, fill_value=False) + .construct(rolling_dim, fill_value=False, stride=self.stride) .sum(dim=rolling_dim, skipna=False) ) return counts + # TODO: verify if _bottleneck_reduce needs adjustments for stride def _bottleneck_reduce(self, func, **kwargs): from .dataarray import DataArray @@ -365,7 +375,7 @@ def _numpy_or_bottleneck_reduce( class DatasetRolling(Rolling): __slots__ = ("rollings",) - def __init__(self, obj, windows, min_periods=None, center=False): + def __init__(self, obj, windows, min_periods=None, center=False, stride=1): """ Moving window object for Dataset. You should use Dataset.rolling() method to construct this object @@ -387,6 +397,8 @@ def __init__(self, obj, windows, min_periods=None, center=False): setting min_periods equal to the size of the window. center : boolean, default False Set the labels at the center of the window. + stride : int, default 1 + Stride of the moving window Returns ------- @@ -407,7 +419,9 @@ def __init__(self, obj, windows, min_periods=None, center=False): for key, da in self.obj.data_vars.items(): # keeps rollings only for the dataset depending on slf.dim if self.dim in da.dims: - self.rollings[key] = DataArrayRolling(da, windows, min_periods, center) + self.rollings[key] = DataArrayRolling( + da, windows, min_periods, center, stride=stride + ) def _dataset_implementation(self, func, **kwargs): from .dataset import Dataset From 684b3627475e6120a649b9fc2d16615aaba9db61 Mon Sep 17 00:00:00 2001 From: matthmey Date: Mon, 18 Nov 2019 17:21:07 +0100 Subject: [PATCH 2/8] fixed bug for rolling construct() method --- xarray/core/rolling.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index b8793f7e6ea..b5e5a60ead8 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -201,7 +201,7 @@ def __iter__(self): yield (label, window) - def construct(self, window_dim, stride=1, fill_value=dtypes.NA): + def construct(self, window_dim, stride=None, fill_value=dtypes.NA): """ Convert this rolling object to xr.DataArray, where the window dimension is stacked as a new dimension @@ -241,6 +241,9 @@ def construct(self, window_dim, stride=1, fill_value=dtypes.NA): from .dataarray import DataArray + if stride is None: + stride = self.stride + window = self.obj.variable.rolling_window( self.dim, self.window, window_dim, self.center, fill_value=fill_value ) @@ -373,7 +376,7 @@ def _numpy_or_bottleneck_reduce( class DatasetRolling(Rolling): - __slots__ = ("rollings",) + __slots__ = ("rollings","stride") def __init__(self, obj, windows, min_periods=None, center=False, stride=1): """ @@ -414,6 +417,7 @@ def __init__(self, obj, windows, min_periods=None, center=False, stride=1): super().__init__(obj, windows, min_periods, center) if self.dim not in self.obj.dims: raise KeyError(self.dim) + self.stride = stride # Keep each Rolling object as a dictionary self.rollings = {} for key, da in self.obj.data_vars.items(): @@ -471,7 +475,7 @@ def _numpy_or_bottleneck_reduce( **kwargs, ) - def construct(self, window_dim, stride=1, fill_value=dtypes.NA): + def construct(self, window_dim, stride=None, fill_value=dtypes.NA): """ Convert this rolling object to xr.Dataset, where the window dimension is stacked as a new dimension @@ -492,6 +496,9 @@ def construct(self, window_dim, stride=1, fill_value=dtypes.NA): from .dataset import Dataset + if stride is None: + stride = self.stride + dataset = {} for key, da in self.obj.data_vars.items(): if self.dim in da.dims: From cc074b4fdd3e969b264150c3b655bc114b1430a4 Mon Sep 17 00:00:00 2001 From: matthmey Date: Tue, 10 Dec 2019 11:15:45 +0100 Subject: [PATCH 3/8] tests and bugfixes --- xarray/core/rolling.py | 22 +++++++---- xarray/tests/test_dataarray.py | 67 ++++++++++++++++++++++++++++++++++ xarray/tests/test_dataset.py | 55 ++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 8 deletions(-) diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 3b23f6f66dd..a2d55132956 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -181,18 +181,23 @@ def __init__(self, obj, windows, min_periods=None, center=False, stride=1): """ super().__init__(obj, windows, min_periods=min_periods, center=center) - self.window_labels = self.obj[self.dim] - self.stride = stride + if stride is None: + self.stride = 1 + else: + self.stride = stride + + window_labels = self.obj[self.dim] + self.window_labels = window_labels[:: self.stride] def __iter__(self): - stops = np.arange(1, len(self.window_labels) + 1) + stops = np.arange(1, len(self.window_labels) * self.stride + 1) starts = stops - int(self.window) starts[: int(self.window)] = 0 # apply striding stops = stops[:: self.stride] starts = starts[:: self.stride] - window_labels = self.window_labels[:: self.stride] + window_labels = self.window_labels for (label, start, stop) in zip(window_labels, starts, stops): window = self.obj.isel(**{self.dim: slice(start, stop)}) @@ -318,7 +323,6 @@ def _counts(self): ) return counts - # TODO: verify if _bottleneck_reduce needs adjustments for stride def _bottleneck_reduce(self, func, **kwargs): from .dataarray import DataArray @@ -360,7 +364,7 @@ def _bottleneck_reduce(self, func, **kwargs): values = values[valid] result = DataArray(values, self.obj.coords) - return result + return result.isel(**{self.dim: slice(None, None, self.stride)}) def _numpy_or_bottleneck_reduce( self, array_agg_func, bottleneck_move_func, **kwargs @@ -385,7 +389,7 @@ def _numpy_or_bottleneck_reduce( class DatasetRolling(Rolling): - __slots__ = ("rollings","stride") + __slots__ = ("rollings", "stride") def __init__(self, obj, windows, min_periods=None, center=False, stride=1): """ @@ -445,7 +449,9 @@ def _dataset_implementation(self, func, **kwargs): reduced[key] = func(self.rollings[key], **kwargs) else: reduced[key] = self.obj[key] - return Dataset(reduced, coords=self.obj.coords) + return Dataset(reduced, coords=self.obj.coords).isel( + **{self.dim: slice(None, None, self.stride)} + ) def reduce(self, func, **kwargs): """Reduce the items in this group by applying `func` along some diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index f957316d8ac..614ed173444 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -4304,6 +4304,73 @@ def test_rolling_construct(center, window): assert (da_rolling_mean == 0.0).sum() >= 0 +@pytest.mark.parametrize("center", (True, False)) +@pytest.mark.parametrize("window", (1, 2, 3, 4)) +@pytest.mark.parametrize("stride", (1, 2, None)) +def test_rolling_stride(center, window, stride): + s = pd.Series(np.arange(10)) + da = DataArray.from_series(s) + + s_rolling = s.rolling(window, center=center, min_periods=1).mean() + da_rolling_strided = da.rolling( + index=window, center=center, min_periods=1, stride=stride + ) + + if stride is None: + stride_index = 1 + else: + stride_index = stride + + # with construct + da_rolling_mean = da_rolling_strided.construct("window").mean("window") + np.testing.assert_allclose(s_rolling.values[::stride_index], da_rolling_mean.values) + np.testing.assert_allclose( + s_rolling.index[::stride_index], da_rolling_mean["index"] + ) + np.testing.assert_allclose( + s_rolling.index[::stride_index], da_rolling_mean["index"] + ) + + # with bottleneck + da_rolling_strided_mean = da_rolling_strided.mean() + np.testing.assert_allclose( + s_rolling.values[::stride_index], da_rolling_strided_mean.values + ) + np.testing.assert_allclose( + s_rolling.index[::stride_index], da_rolling_strided_mean["index"] + ) + np.testing.assert_allclose( + s_rolling.index[::stride_index], da_rolling_strided_mean["index"] + ) + + # with fill_value + da_rolling_mean = da_rolling_strided.construct("window", fill_value=0.0).mean( + "window" + ) + assert da_rolling_mean.isnull().sum() == 0 + assert (da_rolling_mean == 0.0).sum() >= 0 + + # with iter + assert len(da_rolling_strided.window_labels) == len(da["index"]) // stride_index + assert_identical(da_rolling_strided.window_labels, da["index"][::stride_index]) + + for i, (label, window_da) in enumerate(da_rolling_strided): + assert label == da["index"].isel(index=i * stride_index) + + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "Mean of empty slice") + actual = da_rolling_strided_mean.isel(index=i) + expected = window_da.mean("index") + + # TODO add assert_allclose_with_nan, which compares nan position + # as well as the closeness of the values. + assert_array_equal(actual.isnull(), expected.isnull()) + if (~actual.isnull()).sum() > 0: + np.allclose( + actual.values, expected.values, + ) + + @pytest.mark.parametrize("da", (1, 2), indirect=True) @pytest.mark.parametrize("center", (True, False)) @pytest.mark.parametrize("min_periods", (None, 1, 2, 3)) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 7db1911621b..16f261d7dfc 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -5651,6 +5651,61 @@ def test_rolling_construct(center, window): assert (ds_rolling_mean["x"] == 0.0).sum() >= 0 +@pytest.mark.parametrize("center", (True, False)) +@pytest.mark.parametrize("window", (1, 2, 3, 4)) +@pytest.mark.parametrize("stride", (1, 2, None)) +def test_rolling_stride(center, window, stride): + df = pd.DataFrame( + { + "x": np.random.randn(20), + "y": np.random.randn(20), + "time": np.linspace(0, 1, 20), + } + ) + ds = Dataset.from_dataframe(df) + + df_rolling = df.rolling(window, center=center, min_periods=1).mean() + ds_rolling_strided = ds.rolling( + index=window, center=center, min_periods=1, stride=stride + ) + + if stride is None: + stride_index = 1 + else: + stride_index = stride + + # with construct + ds_rolling_mean = ds_rolling_strided.construct("window").mean("window") + np.testing.assert_allclose( + df_rolling["x"].values[::stride_index], ds_rolling_mean["x"].values + ) + np.testing.assert_allclose( + df_rolling.index[::stride_index], ds_rolling_mean["index"] + ) + np.testing.assert_allclose( + df_rolling.index[::stride_index], ds_rolling_mean["index"] + ) + + # with bottleneck + ds_rolling_strided_mean = ds_rolling_strided.mean() + np.testing.assert_allclose( + df_rolling["x"].values[::stride_index], ds_rolling_strided_mean["x"].values + ) + np.testing.assert_allclose( + df_rolling.index[::stride_index], ds_rolling_strided_mean["index"] + ) + np.testing.assert_allclose( + df_rolling.index[::stride_index], ds_rolling_strided_mean["index"] + ) + + # with fill_value + ds_rolling_mean = ds_rolling_strided.construct("window", fill_value=0.0).mean( + "window" + ) + assert (ds_rolling_mean.isnull().sum() == 0).to_array(dim="vars").all() + assert (ds_rolling_mean["x"] == 0.0).sum() >= 0 + + @pytest.mark.slow @pytest.mark.parametrize("ds", (1, 2), indirect=True) @pytest.mark.parametrize("center", (True, False)) From 829016186cd8e2c5d21de71b4d2ea8dfe4fadc4f Mon Sep 17 00:00:00 2001 From: matthmey Date: Tue, 10 Dec 2019 12:54:03 +0100 Subject: [PATCH 4/8] added whats new --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 0a3406c3ebe..9b32d4b676b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -31,6 +31,8 @@ New Features - Added the :py:meth:`count` reduction method to both :py:class:`DatasetCoarsen` and :py:class:`DataArrayCoarsen` objects. (:pull:`3500`) By `Deepak Cherian `_ +- :py:meth:`Dataset.rolling` and :py:meth:`DataArray.rolling` now have a stride option + By `Matthias Meyer `_. Bug fixes ~~~~~~~~~ From c1c5b58734086621f69ede7a373daef04cbbc836 Mon Sep 17 00:00:00 2001 From: matthmey Date: Tue, 10 Dec 2019 15:35:00 +0100 Subject: [PATCH 5/8] bug fixed and added zarr group tests --- xarray/backends/zarr.py | 4 ++-- xarray/tests/test_backends.py | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 6d4ebb02a11..1ebe4e71a1f 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -372,7 +372,7 @@ def store( if len(existing_variables) > 0: # there are variables to append # their encoding must be the same as in the store - ds = open_zarr(self.ds.store, chunks=None) + ds = open_zarr(self.ds.store, group=self.ds.path, chunks=None) variables_with_encoding = {} for vn in existing_variables: variables_with_encoding[vn] = variables[vn].copy(deep=False) @@ -486,7 +486,7 @@ def open_zarr( directory in file system where a Zarr DirectoryStore has been stored. synchronizer : object, optional Array synchronizer provided to zarr - group : str, obtional + group : str, optional Group path. (a.k.a. `path` in zarr terminology.) chunks : int or dict or tuple or {None, 'auto'}, optional Chunk sizes along each dimension, e.g., ``5`` or diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index de3a7eadab0..1ff9442139f 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1690,39 +1690,40 @@ def test_hidden_zarr_keys(self): with xr.decode_cf(store): pass - def test_write_persistence_modes(self): + @pytest.mark.parametrize("group", [None, "group1"]) + def test_write_persistence_modes(self,group): original = create_test_data() # overwrite mode - with self.roundtrip(original, save_kwargs={"mode": "w"}) as actual: + with self.roundtrip(original, save_kwargs={"mode": "w","group":group}, open_kwargs={"group":group}) as actual: assert_identical(original, actual) # don't overwrite mode - with self.roundtrip(original, save_kwargs={"mode": "w-"}) as actual: + with self.roundtrip(original, save_kwargs={"mode": "w-","group":group}, open_kwargs={"group":group}) as actual: assert_identical(original, actual) # make sure overwriting works as expected with self.create_zarr_target() as store: self.save(original, store) # should overwrite with no error - self.save(original, store, mode="w") - with self.open(store) as actual: + self.save(original, store, mode="w", group=group) + with self.open(store, group=group) as actual: assert_identical(original, actual) with pytest.raises(ValueError): self.save(original, store, mode="w-") # check append mode for normal write - with self.roundtrip(original, save_kwargs={"mode": "a"}) as actual: + with self.roundtrip(original, save_kwargs={"mode": "a","group":group}, open_kwargs={"group":group}) as actual: assert_identical(original, actual) - ds, ds_to_append, _ = create_append_test_data() - # check append mode for append write + ds, ds_to_append, _ = create_append_test_data() with self.create_zarr_target() as store_target: - ds.to_zarr(store_target, mode="w") - ds_to_append.to_zarr(store_target, append_dim="time") + ds.to_zarr(store_target, mode="w", group=group) + ds_to_append.to_zarr(store_target, append_dim="time", group=group) original = xr.concat([ds, ds_to_append], dim="time") - assert_identical(original, xr.open_zarr(store_target)) + actual = xr.open_zarr(store_target,group=group) + assert_identical(original, actual) def test_compressor_encoding(self): original = create_test_data() From 84d9e9317dee700c1e0bcfafb7fe9fe008c60f05 Mon Sep 17 00:00:00 2001 From: matthmey Date: Tue, 10 Dec 2019 15:39:25 +0100 Subject: [PATCH 6/8] black . --- xarray/tests/test_backends.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 1ff9442139f..b36ae64cf2e 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1691,15 +1691,23 @@ def test_hidden_zarr_keys(self): pass @pytest.mark.parametrize("group", [None, "group1"]) - def test_write_persistence_modes(self,group): + def test_write_persistence_modes(self, group): original = create_test_data() # overwrite mode - with self.roundtrip(original, save_kwargs={"mode": "w","group":group}, open_kwargs={"group":group}) as actual: + with self.roundtrip( + original, + save_kwargs={"mode": "w", "group": group}, + open_kwargs={"group": group}, + ) as actual: assert_identical(original, actual) # don't overwrite mode - with self.roundtrip(original, save_kwargs={"mode": "w-","group":group}, open_kwargs={"group":group}) as actual: + with self.roundtrip( + original, + save_kwargs={"mode": "w-", "group": group}, + open_kwargs={"group": group}, + ) as actual: assert_identical(original, actual) # make sure overwriting works as expected @@ -1713,16 +1721,20 @@ def test_write_persistence_modes(self,group): self.save(original, store, mode="w-") # check append mode for normal write - with self.roundtrip(original, save_kwargs={"mode": "a","group":group}, open_kwargs={"group":group}) as actual: + with self.roundtrip( + original, + save_kwargs={"mode": "a", "group": group}, + open_kwargs={"group": group}, + ) as actual: assert_identical(original, actual) # check append mode for append write - ds, ds_to_append, _ = create_append_test_data() + ds, ds_to_append, _ = create_append_test_data() with self.create_zarr_target() as store_target: ds.to_zarr(store_target, mode="w", group=group) ds_to_append.to_zarr(store_target, append_dim="time", group=group) original = xr.concat([ds, ds_to_append], dim="time") - actual = xr.open_zarr(store_target,group=group) + actual = xr.open_zarr(store_target, group=group) assert_identical(original, actual) def test_compressor_encoding(self): From 8f376f6656f8e5efcc6ac67795953663e8bcd70e Mon Sep 17 00:00:00 2001 From: matthmey Date: Mon, 31 May 2021 14:33:24 +0200 Subject: [PATCH 7/8] black --- .github/workflows/parse_logs.py | 3 +- xarray/backends/api.py | 5 +- xarray/backends/common.py | 5 +- xarray/backends/pydap_.py | 5 +- xarray/backends/pynio_.py | 6 +- xarray/backends/zarr.py | 6 +- xarray/core/accessor_str.py | 159 ++++------------- xarray/core/common.py | 17 +- xarray/core/computation.py | 5 +- xarray/core/dataarray.py | 16 +- xarray/core/dataset.py | 16 +- xarray/core/indexes.py | 6 +- xarray/core/npcompat.py | 6 +- xarray/core/rolling.py | 24 ++- xarray/core/variable.py | 35 +--- xarray/plot/utils.py | 11 +- xarray/tests/test_accessor_dt.py | 13 +- xarray/tests/test_accessor_str.py | 263 ++++++++-------------------- xarray/tests/test_backends.py | 27 +-- xarray/tests/test_backends_api.py | 4 +- xarray/tests/test_computation.py | 212 ++++------------------ xarray/tests/test_concat.py | 3 +- xarray/tests/test_dataarray.py | 6 +- xarray/tests/test_dataset.py | 24 +-- xarray/tests/test_duck_array_ops.py | 4 +- xarray/tests/test_formatting.py | 5 +- xarray/tests/test_interp.py | 11 +- xarray/tests/test_plot.py | 5 +- xarray/tests/test_plugins.py | 11 +- xarray/tests/test_testing.py | 5 +- xarray/tests/test_units.py | 76 ++------ xarray/tests/test_variable.py | 19 +- xarray/tutorial.py | 13 +- 33 files changed, 239 insertions(+), 787 deletions(-) diff --git a/.github/workflows/parse_logs.py b/.github/workflows/parse_logs.py index 545beaa4167..0a72d9ba1c2 100644 --- a/.github/workflows/parse_logs.py +++ b/.github/workflows/parse_logs.py @@ -13,8 +13,7 @@ def extract_short_test_summary_info(lines): up_to_start_of_section = itertools.dropwhile( - lambda l: "=== short test summary info ===" not in l, - lines, + lambda l: "=== short test summary info ===" not in l, lines, ) up_to_section_content = itertools.islice(up_to_start_of_section, 1, None) section_content = itertools.takewhile( diff --git a/xarray/backends/api.py b/xarray/backends/api.py index e950baed5e0..027b84ddd7e 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -494,10 +494,7 @@ def open_dataset( overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None) backend_ds = backend.open_dataset( - filename_or_obj, - drop_variables=drop_variables, - **decoders, - **kwargs, + filename_or_obj, drop_variables=drop_variables, **decoders, **kwargs, ) ds = _dataset_from_backend_dataset( backend_ds, diff --git a/xarray/backends/common.py b/xarray/backends/common.py index 64a245ddead..58468cc25b6 100644 --- a/xarray/backends/common.py +++ b/xarray/backends/common.py @@ -374,10 +374,7 @@ class BackendEntrypoint: """list of ``open_dataset`` method parameters""" def open_dataset( - self, - filename_or_obj: str, - drop_variables: Tuple[str] = None, - **kwargs: Any, + self, filename_or_obj: str, drop_variables: Tuple[str] = None, **kwargs: Any, ): """ Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`. diff --git a/xarray/backends/pydap_.py b/xarray/backends/pydap_.py index 25d2df9d76a..9ca5af66d18 100644 --- a/xarray/backends/pydap_.py +++ b/xarray/backends/pydap_.py @@ -134,10 +134,7 @@ def open_dataset( DeprecationWarning, ) - store = PydapDataStore.open( - filename_or_obj, - session=session, - ) + store = PydapDataStore.open(filename_or_obj, session=session,) store_entrypoint = StoreBackendEntrypoint() with close_on_error(store): diff --git a/xarray/backends/pynio_.py b/xarray/backends/pynio_.py index bb57e0bea81..0a892ad9e35 100644 --- a/xarray/backends/pynio_.py +++ b/xarray/backends/pynio_.py @@ -112,11 +112,7 @@ def open_dataset( mode="r", lock=None, ): - store = NioDataStore( - filename_or_obj, - mode=mode, - lock=lock, - ) + store = NioDataStore(filename_or_obj, mode=mode, lock=lock,) filename_or_obj = _normalize_path(filename_or_obj) store_entrypoint = StoreBackendEntrypoint() diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 72c4e99265d..69da25e8ad1 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -319,11 +319,7 @@ def open_group( if isinstance(store, pathlib.Path): store = os.fspath(store) - open_kwargs = dict( - mode=mode, - synchronizer=synchronizer, - path=group, - ) + open_kwargs = dict(mode=mode, synchronizer=synchronizer, path=group,) if LooseVersion(zarr.__version__) >= "2.5.0": open_kwargs["storage_options"] = storage_options elif storage_options: diff --git a/xarray/core/accessor_str.py b/xarray/core/accessor_str.py index d50163c435b..d711a4a7b5c 100644 --- a/xarray/core/accessor_str.py +++ b/xarray/core/accessor_str.py @@ -207,10 +207,7 @@ class StringAccessor: def __init__(self, obj): self._obj = obj - def _stringify( - self, - invar: Any, - ) -> Union[str, bytes, Any]: + def _stringify(self, invar: Any,) -> Union[str, bytes, Any]: """ Convert a string-like to the correct string/bytes type. @@ -288,31 +285,19 @@ def len(self) -> Any: """ return self._apply(func=len, dtype=int) - def __getitem__( - self, - key: Union[int, slice], - ) -> Any: + def __getitem__(self, key: Union[int, slice],) -> Any: if isinstance(key, slice): return self.slice(start=key.start, stop=key.stop, step=key.step) else: return self.get(key) - def __add__( - self, - other: Any, - ) -> Any: + def __add__(self, other: Any,) -> Any: return self.cat(other, sep="") - def __mul__( - self, - num: Union[int, Any], - ) -> Any: + def __mul__(self, num: Union[int, Any],) -> Any: return self.repeat(num) - def __mod__( - self, - other: Any, - ) -> Any: + def __mod__(self, other: Any,) -> Any: if isinstance(other, dict): other = {key: self._stringify(val) for key, val in other.items()} return self._apply(func=lambda x: x % other) @@ -322,11 +307,7 @@ def __mod__( else: return self._apply(func=lambda x, y: x % y, func_args=(other,)) - def get( - self, - i: Union[int, Any], - default: Union[str, bytes] = "", - ) -> Any: + def get(self, i: Union[int, Any], default: Union[str, bytes] = "",) -> Any: """ Extract character number `i` from each string in the array. @@ -433,11 +414,7 @@ def func(x, istart, istop, irepl): return self._apply(func=func, func_args=(start, stop, repl)) - def cat( - self, - *others, - sep: Union[str, bytes, Any] = "", - ) -> Any: + def cat(self, *others, sep: Union[str, bytes, Any] = "",) -> Any: """ Concatenate strings elementwise in the DataArray with other strings. @@ -512,17 +489,9 @@ def cat( # sep will go at the end of the input arguments. func = lambda *x: x[-1].join(x[:-1]) - return self._apply( - func=func, - func_args=others, - dtype=self._obj.dtype.kind, - ) + return self._apply(func=func, func_args=others, dtype=self._obj.dtype.kind,) - def join( - self, - dim: Hashable = None, - sep: Union[str, bytes, Any] = "", - ) -> Any: + def join(self, dim: Hashable = None, sep: Union[str, bytes, Any] = "",) -> Any: """ Concatenate strings in a DataArray along a particular dimension. @@ -589,11 +558,7 @@ def join( # concatenate the resulting arrays return start.str.cat(*others, sep=sep) - def format( - self, - *args: Any, - **kwargs: Any, - ) -> Any: + def format(self, *args: Any, **kwargs: Any,) -> Any: """ Perform python string formatting on each element of the DataArray. @@ -738,10 +703,7 @@ def casefold(self) -> Any: """ return self._apply(func=lambda x: x.casefold()) - def normalize( - self, - form: str, - ) -> Any: + def normalize(self, form: str,) -> Any: """ Return the Unicode normal form for the strings in the datarray. @@ -860,10 +822,7 @@ def isupper(self) -> Any: return self._apply(func=lambda x: x.isupper(), dtype=bool) def count( - self, - pat: Union[str, bytes, Pattern, Any], - flags: int = 0, - case: bool = None, + self, pat: Union[str, bytes, Pattern, Any], flags: int = 0, case: bool = None, ) -> Any: """ Count occurrences of pattern in each string of the array. @@ -900,10 +859,7 @@ def count( func = lambda x, ipat: len(ipat.findall(x)) return self._apply(func=func, func_args=(pat,), dtype=int) - def startswith( - self, - pat: Union[str, bytes, Any], - ) -> Any: + def startswith(self, pat: Union[str, bytes, Any],) -> Any: """ Test if the start of each string in the array matches a pattern. @@ -926,10 +882,7 @@ def startswith( func = lambda x, y: x.startswith(y) return self._apply(func=func, func_args=(pat,), dtype=bool) - def endswith( - self, - pat: Union[str, bytes, Any], - ) -> Any: + def endswith(self, pat: Union[str, bytes, Any],) -> Any: """ Test if the end of each string in the array matches a pattern. @@ -1012,9 +965,7 @@ def overfunc(x, iwidth, ifillchar): return self._apply(func=overfunc, func_args=(width, fillchar)) def center( - self, - width: Union[int, Any], - fillchar: Union[str, bytes, Any] = " ", + self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad left and right side of each string in the array. @@ -1039,9 +990,7 @@ def center( return self._padder(func=func, width=width, fillchar=fillchar) def ljust( - self, - width: Union[int, Any], - fillchar: Union[str, bytes, Any] = " ", + self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad right side of each string in the array. @@ -1066,9 +1015,7 @@ def ljust( return self._padder(func=func, width=width, fillchar=fillchar) def rjust( - self, - width: Union[int, Any], - fillchar: Union[str, bytes, Any] = " ", + self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad left side of each string in the array. @@ -1189,10 +1136,7 @@ def func(x, ipat): return self._apply(func=func, func_args=(pat,), dtype=bool) def match( - self, - pat: Union[str, bytes, Pattern, Any], - case: bool = None, - flags: int = 0, + self, pat: Union[str, bytes, Pattern, Any], case: bool = None, flags: int = 0, ) -> Any: """ Determine if each string in the array matches a regular expression. @@ -1226,9 +1170,7 @@ def match( return self._apply(func=func, func_args=(pat,), dtype=bool) def strip( - self, - to_strip: Union[str, bytes, Any] = None, - side: str = "both", + self, to_strip: Union[str, bytes, Any] = None, side: str = "both", ) -> Any: """ Remove leading and trailing characters. @@ -1266,10 +1208,7 @@ def strip( return self._apply(func=func, func_args=(to_strip,)) - def lstrip( - self, - to_strip: Union[str, bytes, Any] = None, - ) -> Any: + def lstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: """ Remove leading characters. @@ -1292,10 +1231,7 @@ def lstrip( """ return self.strip(to_strip, side="left") - def rstrip( - self, - to_strip: Union[str, bytes, Any] = None, - ) -> Any: + def rstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: """ Remove trailing characters. @@ -1318,11 +1254,7 @@ def rstrip( """ return self.strip(to_strip, side="right") - def wrap( - self, - width: Union[int, Any], - **kwargs, - ) -> Any: + def wrap(self, width: Union[int, Any], **kwargs,) -> Any: """ Wrap long strings in the array in paragraphs with length less than `width`. @@ -1349,10 +1281,7 @@ def wrap( func = lambda x, itw: "\n".join(itw.wrap(x)) return self._apply(func=func, func_args=(tw,)) - def translate( - self, - table: Mapping[Union[str, bytes], Union[str, bytes]], - ) -> Any: + def translate(self, table: Mapping[Union[str, bytes], Union[str, bytes]],) -> Any: """ Map characters of each string through the given mapping table. @@ -1371,10 +1300,7 @@ def translate( func = lambda x: x.translate(table) return self._apply(func=func) - def repeat( - self, - repeats: Union[int, Any], - ) -> Any: + def repeat(self, repeats: Union[int, Any],) -> Any: """ Repeat each string in the array. @@ -1954,10 +1880,7 @@ def _get_res(val, ipat, imaxcount=maxcount, dtype=self._obj.dtype): ).astype(self._obj.dtype.kind) def findall( - self, - pat: Union[str, bytes, Pattern, Any], - case: bool = None, - flags: int = 0, + self, pat: Union[str, bytes, Pattern, Any], case: bool = None, flags: int = 0, ) -> Any: r""" Find all occurrences of pattern or regular expression in the DataArray. @@ -2047,11 +1970,7 @@ def func(x, ipat): return self._apply(func=func, func_args=(pat,), dtype=np.object_) def _partitioner( - self, - *, - func: Callable, - dim: Hashable, - sep: Optional[Union[str, bytes, Any]], + self, *, func: Callable, dim: Hashable, sep: Optional[Union[str, bytes, Any]], ) -> Any: """ Implements logic for `partition` and `rpartition`. @@ -2079,9 +1998,7 @@ def _partitioner( ).astype(self._obj.dtype.kind) def partition( - self, - dim: Optional[Hashable], - sep: Union[str, bytes, Any] = " ", + self, dim: Optional[Hashable], sep: Union[str, bytes, Any] = " ", ) -> Any: """ Split the strings in the DataArray at the first occurrence of separator `sep`. @@ -2117,9 +2034,7 @@ def partition( return self._partitioner(func=self._obj.dtype.type.partition, dim=dim, sep=sep) def rpartition( - self, - dim: Optional[Hashable], - sep: Union[str, bytes, Any] = " ", + self, dim: Optional[Hashable], sep: Union[str, bytes, Any] = " ", ) -> Any: """ Split the strings in the DataArray at the last occurrence of separator `sep`. @@ -2437,11 +2352,7 @@ def rsplit( maxsplit=maxsplit, ) - def get_dummies( - self, - dim: Hashable, - sep: Union[str, bytes, Any] = "|", - ) -> Any: + def get_dummies(self, dim: Hashable, sep: Union[str, bytes, Any] = "|",) -> Any: """ Return DataArray of dummy/indicator variables. @@ -2516,11 +2427,7 @@ def get_dummies( res.coords[dim] = vals return res - def decode( - self, - encoding: str, - errors: str = "strict", - ) -> Any: + def decode(self, encoding: str, errors: str = "strict",) -> Any: """ Decode character string in the array using indicated encoding. @@ -2546,11 +2453,7 @@ def decode( func = lambda x: decoder(x, errors)[0] return self._apply(func=func, dtype=np.str_) - def encode( - self, - encoding: str, - errors: str = "strict", - ) -> Any: + def encode(self, encoding: str, errors: str = "strict",) -> Any: """ Encode character string in the array using indicated encoding. diff --git a/xarray/core/common.py b/xarray/core/common.py index 34e7766ab43..00148a755e5 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -893,7 +893,12 @@ def rolling( dim = either_dict_or_kwargs(dim, window_kwargs, "rolling") return self._rolling_cls( - self, dim, min_periods=min_periods, center=center, stride=stride, keep_attrs=keep_attrs + self, + dim, + min_periods=min_periods, + center=center, + stride=stride, + keep_attrs=keep_attrs, ) def rolling_exp( @@ -1345,10 +1350,7 @@ def isnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.isnull, - self, - dask="allowed", - keep_attrs=keep_attrs, + duck_array_ops.isnull, self, dask="allowed", keep_attrs=keep_attrs, ) def notnull(self, keep_attrs: bool = None): @@ -1381,10 +1383,7 @@ def notnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.notnull, - self, - dask="allowed", - keep_attrs=keep_attrs, + duck_array_ops.notnull, self, dask="allowed", keep_attrs=keep_attrs, ) def isin(self, test_elements): diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 07b7bca27c5..e25c34fd794 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -752,10 +752,7 @@ def func(*arrays): ) objs = _all_of_type(args, Variable) - attrs = merge_attrs( - [obj.attrs for obj in objs], - combine_attrs=keep_attrs, - ) + attrs = merge_attrs([obj.attrs for obj in objs], combine_attrs=keep_attrs,) output = [] for dims, data in zip(output_dims, result_data): diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 2ab60b894e1..9c452c88a5b 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1949,9 +1949,7 @@ def set_index( return self._from_temp_dataset(ds) def reset_index( - self, - dims_or_levels: Union[Hashable, Sequence[Hashable]], - drop: bool = False, + self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, ) -> Optional["DataArray"]: """Reset the specified index(es) or multi-index level(s). @@ -3003,10 +3001,7 @@ def _unary_op(self, f: Callable, *args, **kwargs): return da def _binary_op( - self, - other, - f: Callable, - reflexive: bool = False, + self, other, f: Callable, reflexive: bool = False, ): if isinstance(other, (Dataset, groupby.GroupBy)): return NotImplemented @@ -4572,12 +4567,7 @@ def curvefit( ) def drop_duplicates( - self, - dim: Hashable, - keep: Union[ - str, - bool, - ] = "first", + self, dim: Hashable, keep: Union[str, bool,] = "first", ): """Returns a new DataArray with duplicate dimension values removed. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 1697a7c67aa..4b6bb51bb7c 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -3297,9 +3297,7 @@ def _rename_all(self, name_dict, dims_dict): return variables, coord_names, dims, indexes def rename( - self, - name_dict: Mapping[Hashable, Hashable] = None, - **names: Hashable, + self, name_dict: Mapping[Hashable, Hashable] = None, **names: Hashable, ) -> "Dataset": """Returns a new object with renamed variables and dimensions. @@ -3721,9 +3719,7 @@ def set_index( return self._replace_vars_and_dims(variables, coord_names=coord_names) def reset_index( - self, - dims_or_levels: Union[Hashable, Sequence[Hashable]], - drop: bool = False, + self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, ) -> "Dataset": """Reset the specified index(es) or multi-index level(s). @@ -6252,9 +6248,7 @@ def differentiate(self, coord, edge_order=1, datetime_unit=None): return self._replace(variables) def integrate( - self, - coord: Union[Hashable, Sequence[Hashable]], - datetime_unit: str = None, + self, coord: Union[Hashable, Sequence[Hashable]], datetime_unit: str = None, ) -> "Dataset": """Integrate along the given coordinate using the trapezoidal rule. @@ -6368,9 +6362,7 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False): ) def cumulative_integrate( - self, - coord: Union[Hashable, Sequence[Hashable]], - datetime_unit: str = None, + self, coord: Union[Hashable, Sequence[Hashable]], datetime_unit: str = None, ) -> "Dataset": """Integrate along the given coordinate using the trapezoidal rule. diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index be362e1c942..4fa94595c20 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -136,11 +136,7 @@ def intersection(self, other): def __getitem__( self, indexer ) -> Union[ - "PandasIndex", - NumpyIndexingAdapter, - np.ndarray, - np.datetime64, - np.timedelta64, + "PandasIndex", NumpyIndexingAdapter, np.ndarray, np.datetime64, np.timedelta64, ]: key = indexer.tuple if isinstance(key, tuple) and len(key) == 1: diff --git a/xarray/core/npcompat.py b/xarray/core/npcompat.py index 35bac982d4c..c8880c426bf 100644 --- a/xarray/core/npcompat.py +++ b/xarray/core/npcompat.py @@ -112,12 +112,10 @@ def __array__(self) -> np.ndarray: ] _RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]] _ArrayLike = Union[ - _NestedSequence[_SupportsArray], - _NestedSequence[_T], + _NestedSequence[_SupportsArray], _NestedSequence[_T], ] _ArrayLikeFallback = Union[ - _ArrayLike[Union[bool, int, float, complex, str, bytes]], - _RecursiveSequence, + _ArrayLike[Union[bool, int, float, complex, str, bytes]], _RecursiveSequence, ] # The extra step defining _ArrayLikeFallback and using ArrayLike as a type # alias for it works around an issue with mypy. diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 26ba58fd241..4116ad39e35 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -202,7 +202,9 @@ def _get_keep_attrs(self, keep_attrs): class DataArrayRolling(Rolling): __slots__ = ("window_labels", "stride") - def __init__(self, obj, windows, min_periods=None, center=False, stride=1, keep_attrs=None): + def __init__( + self, obj, windows, min_periods=None, center=False, stride=1, keep_attrs=None + ): """ Moving window object for DataArray. You should use DataArray.rolling() method to construct this object @@ -454,7 +456,11 @@ def reduce(self, func, keep_attrs=None, **kwargs): else: obj = self.obj windows = self._construct( - obj, rolling_dim, keep_attrs=keep_attrs, fill_value=fillna, stride=self.stride + obj, + rolling_dim, + keep_attrs=keep_attrs, + fill_value=fillna, + stride=self.stride, ) result = windows.reduce( @@ -482,7 +488,9 @@ def _counts(self, keep_attrs): center={d: self.center[i] for i, d in enumerate(self.dim)}, **{d: w for d, w in zip(self.dim, self.window)}, ) - .construct(rolling_dim, fill_value=False, stride=self.stride, keep_attrs=keep_attrs) + .construct( + rolling_dim, fill_value=False, stride=self.stride, keep_attrs=keep_attrs + ) .sum(dim=list(rolling_dim.values()), skipna=False, keep_attrs=keep_attrs) ) return counts @@ -525,7 +533,7 @@ def _bottleneck_reduce(self, func, keep_attrs, **kwargs): if self.center[0]: values = values[valid] - values = values.isel(**{self.dim: slice(None, None, self.stride)}) + values = values.isel(**{self.dim: slice(None, None, self.stride)}) attrs = self.obj.attrs if keep_attrs else {} return DataArray(values, self.obj.coords, attrs=attrs, name=self.obj.name) @@ -576,7 +584,9 @@ def _numpy_or_bottleneck_reduce( class DatasetRolling(Rolling): __slots__ = ("rollings", "stride") - def __init__(self, obj, windows, min_periods=None, center=False, stride=1, keep_attrs=None): + def __init__( + self, obj, windows, min_periods=None, center=False, stride=1, keep_attrs=None + ): """ Moving window object for Dataset. You should use Dataset.rolling() method to construct this object @@ -625,7 +635,9 @@ def __init__(self, obj, windows, min_periods=None, center=False, stride=1, keep_ if dims: w = {d: windows[d] for d in dims} - self.rollings[key] = DataArrayRolling(da, w, min_periods, center, stride=stride) + self.rollings[key] = DataArrayRolling( + da, w, min_periods, center, stride=stride + ) def _dataset_implementation(self, func, keep_attrs, **kwargs): from .dataset import Dataset diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 1abaad49cdf..418c613659e 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -48,12 +48,7 @@ ) NON_NUMPY_SUPPORTED_ARRAY_TYPES = ( - ( - indexing.ExplicitlyIndexed, - pd.Index, - ) - + dask_array_type - + cupy_array_type + (indexing.ExplicitlyIndexed, pd.Index,) + dask_array_type + cupy_array_type ) # https://github.com/python/mypy/issues/224 BASIC_INDEXING_TYPES = integer_types + (slice,) @@ -1561,10 +1556,7 @@ def _unstack_once_full( return Variable(new_dims, new_data, self._attrs, self._encoding, fastpath=True) def _unstack_once( - self, - index: pd.MultiIndex, - dim: Hashable, - fill_value=dtypes.NA, + self, index: pd.MultiIndex, dim: Hashable, fill_value=dtypes.NA, ) -> "Variable": """ Unstacks this variable given an index to unstack and the name of the @@ -1593,10 +1585,7 @@ def _unstack_once( dtype = self.dtype data = np.full_like( - self.data, - fill_value=fill_value, - shape=new_shape, - dtype=dtype, + self.data, fill_value=fill_value, shape=new_shape, dtype=dtype, ) # Indexer is a list of lists of locations. Each list is the locations @@ -1668,13 +1657,7 @@ def clip(self, min=None, max=None): return apply_ufunc(np.clip, self, min, max, dask="allowed") def reduce( - self, - func, - dim=None, - axis=None, - keep_attrs=None, - keepdims=False, - **kwargs, + self, func, dim=None, axis=None, keep_attrs=None, keepdims=False, **kwargs, ): """Reduce this array by applying `func` along some dimension(s). @@ -2260,10 +2243,7 @@ def isnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.isnull, - self, - dask="allowed", - keep_attrs=keep_attrs, + duck_array_ops.isnull, self, dask="allowed", keep_attrs=keep_attrs, ) def notnull(self, keep_attrs: bool = None): @@ -2294,10 +2274,7 @@ def notnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.notnull, - self, - dask="allowed", - keep_attrs=keep_attrs, + duck_array_ops.notnull, self, dask="allowed", keep_attrs=keep_attrs, ) @property diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 858695ec538..78b459ca270 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -805,10 +805,13 @@ def _process_cmap_cbar_kwargs( if func.__name__ == "surface": # Leave user to specify cmap settings for surface plots kwargs["cmap"] = cmap - return { - k: kwargs.get(k, None) - for k in ["vmin", "vmax", "cmap", "extend", "levels", "norm"] - }, {} + return ( + { + k: kwargs.get(k, None) + for k in ["vmin", "vmax", "cmap", "extend", "levels", "norm"] + }, + {}, + ) cbar_kwargs = {} if cbar_kwargs is None else dict(cbar_kwargs) diff --git a/xarray/tests/test_accessor_dt.py b/xarray/tests/test_accessor_dt.py index 62da3bab2cd..ce9c7e3451c 100644 --- a/xarray/tests/test_accessor_dt.py +++ b/xarray/tests/test_accessor_dt.py @@ -90,11 +90,7 @@ def test_field_access(self, field): @pytest.mark.parametrize( "field, pandas_field", - [ - ("year", "year"), - ("week", "week"), - ("weekday", "day"), - ], + [("year", "year"), ("week", "week"), ("weekday", "day"),], ) def test_isocalendar(self, field, pandas_field): @@ -175,12 +171,7 @@ def test_dask_field_access(self, field): @requires_dask @pytest.mark.parametrize( - "field", - [ - "year", - "week", - "weekday", - ], + "field", ["year", "week", "weekday",], ) def test_isocalendar_dask(self, field): import dask.array as da diff --git a/xarray/tests/test_accessor_str.py b/xarray/tests/test_accessor_str.py index 519ca762c41..31ca7388a43 100644 --- a/xarray/tests/test_accessor_str.py +++ b/xarray/tests/test_accessor_str.py @@ -834,15 +834,11 @@ def test_extract_multi_nocase(dtype): def test_extract_broadcast(dtype): - value = xr.DataArray( - ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], - dims=["X"], - ).astype(dtype) + value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) - pat_str = xr.DataArray( - [r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], - dims=["Y"], - ).astype(dtype) + pat_str = xr.DataArray([r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], dims=["Y"],).astype( + dtype + ) pat_re = value.str._re_compile(pat=pat_str) expected = [ @@ -1165,15 +1161,11 @@ def test_extractall_multi_multi_nocase(dtype): def test_extractall_broadcast(dtype): - value = xr.DataArray( - ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], - dims=["X"], - ).astype(dtype) + value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) - pat_str = xr.DataArray( - [r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], - dims=["Y"], - ).astype(dtype) + pat_str = xr.DataArray([r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], dims=["Y"],).astype( + dtype + ) pat_re = value.str._re_compile(pat=pat_str) expected = [ @@ -1262,11 +1254,7 @@ def test_findall_single_multi_case(dtype): expected = [ [["a"], ["bab", "baab"], ["abc", "cbc"]], - [ - ["abcd", "dcd", "dccd"], - [], - ["abcdef", "fef"], - ], + [["abcd", "dcd", "dccd"], [], ["abcdef", "fef"],], ] expected = [[[dtype(x) for x in y] for y in z] for z in expected] expected = np.array(expected, dtype=np.object_) @@ -1302,16 +1290,8 @@ def test_findall_single_multi_nocase(dtype): ).astype(dtype) expected = [ - [ - ["a"], - ["ab", "bab", "baab"], - ["abc", "cbc"], - ], - [ - ["abcd", "dcd", "dccd"], - [], - ["abcdef", "fef"], - ], + [["a"], ["ab", "bab", "baab"], ["abc", "cbc"],], + [["abcd", "dcd", "dccd"], [], ["abcdef", "fef"],], ] expected = [[[dtype(x) for x in y] for y in z] for z in expected] expected = np.array(expected, dtype=np.object_) @@ -1472,15 +1452,11 @@ def test_findall_multi_multi_nocase(dtype): def test_findall_broadcast(dtype): - value = xr.DataArray( - ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], - dims=["X"], - ).astype(dtype) + value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) - pat_str = xr.DataArray( - [r"(\w+)_Xy_\d*", r"\w+_Xy_(\d*)"], - dims=["Y"], - ).astype(dtype) + pat_str = xr.DataArray([r"(\w+)_Xy_\d*", r"\w+_Xy_(\d*)"], dims=["Y"],).astype( + dtype + ) pat_re = value.str._re_compile(pat=pat_str) expected = [[["a"], ["0"]], [[], []], [["abc"], ["01"]]] @@ -2429,11 +2405,7 @@ def test_partition_whitespace(dtype): ).astype(dtype) exp_part_dim = [ - [ - ["abc", " ", "def"], - ["spam", " ", "eggs swallow"], - ["red_blue", "", ""], - ], + [["abc", " ", "def"], ["spam", " ", "eggs swallow"], ["red_blue", "", ""],], [ ["test0", " ", "test1 test2 test3"], ["", "", ""], @@ -2442,11 +2414,7 @@ def test_partition_whitespace(dtype): ] exp_rpart_dim = [ - [ - ["abc", " ", "def"], - ["spam eggs", " ", "swallow"], - ["", "", "red_blue"], - ], + [["abc", " ", "def"], ["spam eggs", " ", "swallow"], ["", "", "red_blue"],], [ ["test0 test1 test2", " ", "test3"], ["", "", ""], @@ -2477,11 +2445,7 @@ def test_partition_comma(dtype): ).astype(dtype) exp_part_dim = [ - [ - ["abc", ", ", "def"], - ["spam", ", ", "eggs, swallow"], - ["red_blue", "", ""], - ], + [["abc", ", ", "def"], ["spam", ", ", "eggs, swallow"], ["red_blue", "", ""],], [ ["test0", ", ", "test1, test2, test3"], ["", "", ""], @@ -2490,11 +2454,7 @@ def test_partition_comma(dtype): ] exp_rpart_dim = [ - [ - ["abc", ", ", "def"], - ["spam, eggs", ", ", "swallow"], - ["", "", "red_blue"], - ], + [["abc", ", ", "def"], ["spam, eggs", ", ", "swallow"], ["", "", "red_blue"],], [ ["test0, test1, test2", ", ", "test3"], ["", "", ""], @@ -2803,14 +2763,10 @@ def test_split_comma(dtype): def test_splitters_broadcast(dtype): values = xr.DataArray( - ["ab cd,de fg", "spam, ,eggs swallow", "red_blue"], - dims=["X"], + ["ab cd,de fg", "spam, ,eggs swallow", "red_blue"], dims=["X"], ).astype(dtype) - sep = xr.DataArray( - [" ", ","], - dims=["Y"], - ).astype(dtype) + sep = xr.DataArray([" ", ","], dims=["Y"],).astype(dtype) expected_left = xr.DataArray( [ @@ -2920,15 +2876,9 @@ def test_get_dummies(dtype): def test_get_dummies_broadcast(dtype): - values = xr.DataArray( - ["x~x|x~x", "x", "x|x~x", "x~x"], - dims=["X"], - ).astype(dtype) + values = xr.DataArray(["x~x|x~x", "x", "x|x~x", "x~x"], dims=["X"],).astype(dtype) - sep = xr.DataArray( - ["|", "~"], - dims=["Y"], - ).astype(dtype) + sep = xr.DataArray(["|", "~"], dims=["Y"],).astype(dtype) expected = [ [[False, False, True], [True, True, False]], @@ -2958,10 +2908,7 @@ def test_get_dummies_empty(dtype): def test_splitters_empty_str(dtype): - values = xr.DataArray( - [["", "", ""], ["", "", ""]], - dims=["X", "Y"], - ).astype(dtype) + values = xr.DataArray([["", "", ""], ["", "", ""]], dims=["X", "Y"],).astype(dtype) targ_partition_dim = xr.DataArray( [ @@ -2980,18 +2927,13 @@ def test_splitters_empty_str(dtype): ] targ_partition_none = np.array(targ_partition_none, dtype=np.object_) del targ_partition_none[-1, -1][-1] - targ_partition_none = xr.DataArray( - targ_partition_none, - dims=["X", "Y"], - ) + targ_partition_none = xr.DataArray(targ_partition_none, dims=["X", "Y"],) targ_split_dim = xr.DataArray( - [[[""], [""], [""]], [[""], [""], [""]]], - dims=["X", "Y", "ZZ"], + [[[""], [""], [""]], [[""], [""], [""]]], dims=["X", "Y", "ZZ"], ).astype(dtype) targ_split_none = xr.DataArray( - np.array([[[], [], []], [[], [], [""]]], dtype=np.object_), - dims=["X", "Y"], + np.array([[[], [], []], [[], [], [""]]], dtype=np.object_), dims=["X", "Y"], ) del targ_split_none.data[-1, -1][-1] @@ -3034,8 +2976,7 @@ def test_splitters_empty_str(dtype): def test_cat_str(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], - dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], ).astype(dtype) values_2 = "111" @@ -3080,12 +3021,10 @@ def test_cat_str(dtype): def test_cat_uniform(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], - dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], ).astype(dtype) values_2 = xr.DataArray( - [["11111", "222", "33"], ["4", "5555", "66"]], - dims=["X", "Y"], + [["11111", "222", "33"], ["4", "5555", "66"]], dims=["X", "Y"], ) targ_blank = xr.DataArray( @@ -3129,13 +3068,9 @@ def test_cat_uniform(dtype): def test_cat_broadcast_right(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], - dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], ).astype(dtype) - values_2 = xr.DataArray( - ["11111", "222", "33"], - dims=["Y"], - ) + values_2 = xr.DataArray(["11111", "222", "33"], dims=["Y"],) targ_blank = xr.DataArray( [["a11111", "bb222", "cccc33"], ["ddddd11111", "eeee222", "fff33"]], @@ -3177,13 +3112,9 @@ def test_cat_broadcast_right(dtype): def test_cat_broadcast_left(dtype): - values_1 = xr.DataArray( - ["a", "bb", "cccc"], - dims=["Y"], - ).astype(dtype) + values_1 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) values_2 = xr.DataArray( - [["11111", "222", "33"], ["4", "5555", "66"]], - dims=["X", "Y"], + [["11111", "222", "33"], ["4", "5555", "66"]], dims=["X", "Y"], ) targ_blank = ( @@ -3242,14 +3173,8 @@ def test_cat_broadcast_left(dtype): def test_cat_broadcast_both(dtype): - values_1 = xr.DataArray( - ["a", "bb", "cccc"], - dims=["Y"], - ).astype(dtype) - values_2 = xr.DataArray( - ["11111", "4"], - dims=["X"], - ) + values_1 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) + values_2 = xr.DataArray(["11111", "4"], dims=["X"],) targ_blank = ( xr.DataArray( @@ -3307,15 +3232,9 @@ def test_cat_broadcast_both(dtype): def test_cat_multi(): - values_1 = xr.DataArray( - ["11111", "4"], - dims=["X"], - ) + values_1 = xr.DataArray(["11111", "4"], dims=["X"],) - values_2 = xr.DataArray( - ["a", "bb", "cccc"], - dims=["Y"], - ).astype(np.bytes_) + values_2 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(np.bytes_) values_3 = np.array(3.4) @@ -3323,10 +3242,7 @@ def test_cat_multi(): values_5 = np.array("", dtype=np.unicode_) - sep = xr.DataArray( - [" ", ", "], - dims=["ZZ"], - ).astype(np.unicode_) + sep = xr.DataArray([" ", ", "], dims=["ZZ"],).astype(np.unicode_) expected = xr.DataArray( [ @@ -3366,10 +3282,7 @@ def test_join_scalar(dtype): def test_join_vector(dtype): - values = xr.DataArray( - ["a", "bb", "cccc"], - dims=["Y"], - ).astype(dtype) + values = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) targ_blank = xr.DataArray("abbcccc").astype(dtype) targ_space = xr.DataArray("a bb cccc").astype(dtype) @@ -3393,27 +3306,20 @@ def test_join_vector(dtype): def test_join_2d(dtype): values = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], - dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], ).astype(dtype) - targ_blank_x = xr.DataArray( - ["addddd", "bbeeee", "ccccfff"], - dims=["Y"], - ).astype(dtype) - targ_space_x = xr.DataArray( - ["a ddddd", "bb eeee", "cccc fff"], - dims=["Y"], - ).astype(dtype) + targ_blank_x = xr.DataArray(["addddd", "bbeeee", "ccccfff"], dims=["Y"],).astype( + dtype + ) + targ_space_x = xr.DataArray(["a ddddd", "bb eeee", "cccc fff"], dims=["Y"],).astype( + dtype + ) - targ_blank_y = xr.DataArray( - ["abbcccc", "dddddeeeefff"], - dims=["X"], - ).astype(dtype) - targ_space_y = xr.DataArray( - ["a bb cccc", "ddddd eeee fff"], - dims=["X"], - ).astype(dtype) + targ_blank_y = xr.DataArray(["abbcccc", "dddddeeeefff"], dims=["X"],).astype(dtype) + targ_space_y = xr.DataArray(["a bb cccc", "ddddd eeee fff"], dims=["X"],).astype( + dtype + ) res_blank_x = values.str.join(dim="X") res_blank_y = values.str.join(dim="Y") @@ -3438,20 +3344,11 @@ def test_join_2d(dtype): def test_join_broadcast(dtype): - values = xr.DataArray( - ["a", "bb", "cccc"], - dims=["X"], - ).astype(dtype) + values = xr.DataArray(["a", "bb", "cccc"], dims=["X"],).astype(dtype) - sep = xr.DataArray( - [" ", ", "], - dims=["ZZ"], - ).astype(dtype) + sep = xr.DataArray([" ", ", "], dims=["ZZ"],).astype(dtype) - expected = xr.DataArray( - ["a bb cccc", "a, bb, cccc"], - dims=["ZZ"], - ).astype(dtype) + expected = xr.DataArray(["a bb cccc", "a, bb, cccc"], dims=["ZZ"],).astype(dtype) res = values.str.join(sep=sep) @@ -3461,8 +3358,7 @@ def test_join_broadcast(dtype): def test_format_scalar(): values = xr.DataArray( - ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], - dims=["X"], + ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], dims=["X"], ).astype(np.unicode_) pos0 = 1 @@ -3474,8 +3370,7 @@ def test_format_scalar(): W = "NO!" expected = xr.DataArray( - ["1.X.None", "1,1.2,'test','test'", "'test'-X-None"], - dims=["X"], + ["1.X.None", "1,1.2,'test','test'", "'test'-X-None"], dims=["X"], ).astype(np.unicode_) res = values.str.format(pos0, pos1, pos2, X=X, Y=Y, ZZ=ZZ, W=W) @@ -3486,17 +3381,13 @@ def test_format_scalar(): def test_format_broadcast(): values = xr.DataArray( - ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], - dims=["X"], + ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], dims=["X"], ).astype(np.unicode_) pos0 = 1 pos1 = 1.2 - pos2 = xr.DataArray( - ["2.3", "3.44444"], - dims=["YY"], - ) + pos2 = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) X = "'test'" Y = "X" @@ -3519,18 +3410,16 @@ def test_format_broadcast(): def test_mod_scalar(): - values = xr.DataArray( - ["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], - dims=["X"], - ).astype(np.unicode_) + values = xr.DataArray(["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], dims=["X"],).astype( + np.unicode_ + ) pos0 = 1 pos1 = 1.2 pos2 = "2.3" expected = xr.DataArray( - ["1.1.2.2.3", "1,1.2,2.3", "1-1.2-2.3"], - dims=["X"], + ["1.1.2.2.3", "1,1.2,2.3", "1-1.2-2.3"], dims=["X"], ).astype(np.unicode_) res = values.str % (pos0, pos1, pos2) @@ -3541,8 +3430,7 @@ def test_mod_scalar(): def test_mod_dict(): values = xr.DataArray( - ["%(a)s.%(a)s.%(b)s", "%(b)s,%(c)s,%(b)s", "%(c)s-%(b)s-%(a)s"], - dims=["X"], + ["%(a)s.%(a)s.%(b)s", "%(b)s,%(c)s,%(b)s", "%(c)s-%(b)s-%(a)s"], dims=["X"], ).astype(np.unicode_) a = 1 @@ -3550,8 +3438,7 @@ def test_mod_dict(): c = "2.3" expected = xr.DataArray( - ["1.1.1.2", "1.2,2.3,1.2", "2.3-1.2-1"], - dims=["X"], + ["1.1.1.2", "1.2,2.3,1.2", "2.3-1.2-1"], dims=["X"], ).astype(np.unicode_) res = values.str % {"a": a, "b": b, "c": c} @@ -3561,15 +3448,9 @@ def test_mod_dict(): def test_mod_broadcast_single(): - values = xr.DataArray( - ["%s_1", "%s_2", "%s_3"], - dims=["X"], - ).astype(np.unicode_) + values = xr.DataArray(["%s_1", "%s_2", "%s_3"], dims=["X"],).astype(np.unicode_) - pos = xr.DataArray( - ["2.3", "3.44444"], - dims=["YY"], - ) + pos = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) expected = xr.DataArray( [["2.3_1", "3.44444_1"], ["2.3_2", "3.44444_2"], ["2.3_3", "3.44444_3"]], @@ -3583,18 +3464,14 @@ def test_mod_broadcast_single(): def test_mod_broadcast_multi(): - values = xr.DataArray( - ["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], - dims=["X"], - ).astype(np.unicode_) + values = xr.DataArray(["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], dims=["X"],).astype( + np.unicode_ + ) pos0 = 1 pos1 = 1.2 - pos2 = xr.DataArray( - ["2.3", "3.44444"], - dims=["YY"], - ) + pos2 = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) expected = xr.DataArray( [ diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 908d2f4940d..b9adc9df2e0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -879,11 +879,7 @@ def _create_cf_dataset(): np.arange(0.1, 0.9, 0.1).reshape(2, 2, 2), {"standard_name": "standard_error"}, ), - det_lim=( - (), - 0.1, - {"standard_name": "detection_minimum"}, - ), + det_lim=((), 0.1, {"standard_name": "detection_minimum"},), ), dict( latitude=("latitude", [0, 1], {"units": "degrees_north"}), @@ -2826,8 +2822,7 @@ def test_open_fileobj(self): with open(tmp_file, "rb") as f: f.seek(8) with pytest.raises( - ValueError, - match="match in any of xarray's currently installed IO", + ValueError, match="match in any of xarray's currently installed IO", ): with pytest.warns( RuntimeWarning, @@ -5126,14 +5121,7 @@ def test_open_dataset_chunking_zarr(chunks, tmp_path): dask_arr = da.from_array( np.ones((500, 500), dtype="float64"), chunks=encoded_chunks ) - ds = xr.Dataset( - { - "test": xr.DataArray( - dask_arr, - dims=("x", "y"), - ) - } - ) + ds = xr.Dataset({"test": xr.DataArray(dask_arr, dims=("x", "y"),)}) ds["test"].encoding["chunks"] = encoded_chunks ds.to_zarr(tmp_path / "test.zarr") @@ -5156,14 +5144,7 @@ def test_chunking_consintency(chunks, tmp_path): dask_arr = da.from_array( np.ones((500, 500), dtype="float64"), chunks=encoded_chunks ) - ds = xr.Dataset( - { - "test": xr.DataArray( - dask_arr, - dims=("x", "y"), - ) - } - ) + ds = xr.Dataset({"test": xr.DataArray(dask_arr, dims=("x", "y"),)}) ds["test"].encoding["chunks"] = encoded_chunks ds.to_zarr(tmp_path / "test.zarr") ds.to_netcdf(tmp_path / "test.nc") diff --git a/xarray/tests/test_backends_api.py b/xarray/tests/test_backends_api.py index 4124d0d0b81..24cd6548687 100644 --- a/xarray/tests/test_backends_api.py +++ b/xarray/tests/test_backends_api.py @@ -26,9 +26,7 @@ def test_custom_engine(): class CustomBackend(xr.backends.BackendEntrypoint): def open_dataset( - filename_or_obj, - drop_variables=None, - **kwargs, + filename_or_obj, drop_variables=None, **kwargs, ): return expected.copy(deep=True) diff --git a/xarray/tests/test_computation.py b/xarray/tests/test_computation.py index b7ae1ca9828..9826a919390 100644 --- a/xarray/tests/test_computation.py +++ b/xarray/tests/test_computation.py @@ -476,13 +476,10 @@ def test_unified_dim_sizes(): "x": 1, "y": 2, } - assert ( - unified_dim_sizes( - [xr.Variable(("x", "z"), [[1]]), xr.Variable(("y", "z"), [[1, 2], [3, 4]])], - exclude_dims={"z"}, - ) - == {"x": 1, "y": 2} - ) + assert unified_dim_sizes( + [xr.Variable(("x", "z"), [[1]]), xr.Variable(("y", "z"), [[1, 2], [3, 4]])], + exclude_dims={"z"}, + ) == {"x": 1, "y": 2} # duplicate dimensions with pytest.raises(ValueError): @@ -567,41 +564,13 @@ def add(a, b, keep_attrs): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( + pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), + pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), + pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - None, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="default", - ), - pytest.param( - False, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="False", - ), - pytest.param( - True, - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="True", - ), - pytest.param( - "override", - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="override", - ), - pytest.param( - "drop", - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="drop", + "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", ), + pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -636,41 +605,13 @@ def test_keep_attrs_strategies_variable(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( + pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), + pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), + pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - None, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="default", - ), - pytest.param( - False, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="False", - ), - pytest.param( - True, - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="True", - ), - pytest.param( - "override", - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="override", - ), - pytest.param( - "drop", - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="drop", + "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", ), + pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -706,41 +647,13 @@ def test_keep_attrs_strategies_dataarray(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( + pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), + pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), + pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - None, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="default", - ), - pytest.param( - False, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="False", - ), - pytest.param( - True, - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="True", - ), - pytest.param( - "override", - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="override", - ), - pytest.param( - "drop", - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="drop", + "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", ), + pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -801,41 +714,13 @@ def test_keep_attrs_strategies_dataarray_variables( @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( + pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), + pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), + pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - None, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="default", - ), - pytest.param( - False, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="False", - ), - pytest.param( - True, - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="True", - ), - pytest.param( - "override", - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="override", - ), - pytest.param( - "drop", - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="drop", + "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", ), + pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -871,41 +756,13 @@ def test_keep_attrs_strategies_dataset(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( + pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), + pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), + pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - None, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="default", - ), - pytest.param( - False, - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="False", - ), - pytest.param( - True, - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="True", - ), - pytest.param( - "override", - [{"a": 1}, {"a": 2}, {"a": 3}], - {"a": 1}, - False, - id="override", - ), - pytest.param( - "drop", - [{"a": 1}, {"a": 2}, {"a": 3}], - {}, - False, - id="drop", + "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", ), + pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -1296,10 +1153,7 @@ def test_vectorize_dask_dtype_without_output_dtypes(data_array): expected = data_array.copy() actual = apply_ufunc( - identity, - data_array.chunk({"x": 1}), - vectorize=True, - dask="parallelized", + identity, data_array.chunk({"x": 1}), vectorize=True, dask="parallelized", ) assert_identical(expected, actual) @@ -1531,8 +1385,7 @@ def np_corr(ts1, ts2): @pytest.mark.parametrize( - "da_a, da_b", - arrays_w_tuples()[1], + "da_a, da_b", arrays_w_tuples()[1], ) @pytest.mark.parametrize("dim", [None, "time", "x"]) def test_covcorr_consistency(da_a, da_b, dim): @@ -1552,8 +1405,7 @@ def test_covcorr_consistency(da_a, da_b, dim): @pytest.mark.parametrize( - "da_a", - arrays_w_tuples()[0], + "da_a", arrays_w_tuples()[0], ) @pytest.mark.parametrize("dim", [None, "time", "x", ["time", "x"]]) def test_autocov(da_a, dim): diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index 42232f7df57..1b59a6bcc34 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -715,8 +715,7 @@ def test_concat_preserve_coordinate_order(): ) expected = Dataset( - {"data": (["time", "y", "x"], data)}, - coords={"time": time, "y": y, "x": x}, + {"data": (["time", "y", "x"], data)}, coords={"time": time, "y": y, "x": x}, ) actual = concat([ds1, ds2], dim="time") diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 356807fc1be..1261e47a6ca 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6501,11 +6501,7 @@ def test_isin(da): @pytest.mark.parametrize( - "funcname, argument", - [ - ("reduce", (np.mean,)), - ("mean", ()), - ], + "funcname, argument", [("reduce", (np.mean,)), ("mean", ()),], ) def test_coarsen_keep_attrs(funcname, argument): attrs_da = {"da_attr": "test"} diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 5907790db1f..46f1fe1b401 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -6291,11 +6291,7 @@ def test_coarsen_coords_cftime(): @pytest.mark.parametrize( - "funcname, argument", - [ - ("reduce", (np.mean,)), - ("mean", ()), - ], + "funcname, argument", [("reduce", (np.mean,)), ("mean", ()),], ) def test_coarsen_keep_attrs(funcname, argument): global_attrs = {"units": "test", "long_name": "testing"} @@ -6379,9 +6375,7 @@ def test_coarsen_keep_attrs_deprecated(): coords = np.linspace(1, 10, 100) ds = Dataset( - data_vars={"da": ("coord", data)}, - coords={"coord": coords}, - attrs=global_attrs, + data_vars={"da": ("coord", data)}, coords={"coord": coords}, attrs=global_attrs, ) ds.da.attrs = attrs_da @@ -6509,9 +6503,7 @@ def test_rolling_keep_attrs_deprecated(): coords = np.linspace(1, 10, 100) ds = Dataset( - data_vars={"da": ("coord", data)}, - coords={"coord": coords}, - attrs=global_attrs, + data_vars={"da": ("coord", data)}, coords={"coord": coords}, attrs=global_attrs, ) ds.da.attrs = attrs_da @@ -7072,8 +7064,7 @@ def test_cumulative_integrate(dask): ) assert_allclose(expected_x, actual.compute()) assert_equal( - ds["var"].cumulative_integrate("x"), - ds.cumulative_integrate("x")["var"], + ds["var"].cumulative_integrate("x"), ds.cumulative_integrate("x")["var"], ) # make sure result is also a dask array (if the source is dask array) @@ -7082,15 +7073,12 @@ def test_cumulative_integrate(dask): # along y actual = da.cumulative_integrate("y") expected_y = xr.DataArray( - cumtrapz(da, da["y"], axis=1, initial=0.0), - dims=["x", "y"], - coords=da.coords, + cumtrapz(da, da["y"], axis=1, initial=0.0), dims=["x", "y"], coords=da.coords, ) assert_allclose(expected_y, actual.compute()) assert_equal(actual, ds.cumulative_integrate("y")["var"]) assert_equal( - ds["var"].cumulative_integrate("y"), - ds.cumulative_integrate("y")["var"], + ds["var"].cumulative_integrate("y"), ds.cumulative_integrate("y")["var"], ) # along x and y diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 0eb007259bb..b2421f32942 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -889,8 +889,6 @@ def test_push_dask(): # some chunks of size-1 with NaN with raise_if_dask_computes(): actual = push( - dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)), - axis=0, - n=None, + dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)), axis=0, n=None, ) np.testing.assert_equal(actual, expected) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index b9ba57f99dc..d403f7319ae 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -514,10 +514,7 @@ def test__mapping_repr(display_max_rows, n_vars, n_attr): data_vars = dict() for v in a: data_vars[v] = xr.DataArray( - name=v, - data=np.array([3, 4]), - dims=["time"], - coords=coords, + name=v, data=np.array([3, 4]), dims=["time"], coords=coords, ) ds = xr.Dataset(data_vars) ds.attrs = attrs diff --git a/xarray/tests/test_interp.py b/xarray/tests/test_interp.py index ab023dc1558..121aa596759 100644 --- a/xarray/tests/test_interp.py +++ b/xarray/tests/test_interp.py @@ -851,15 +851,13 @@ def test_interpolate_chunk_advanced(method): theta = np.linspace(0, 2 * np.pi, 5) w = np.linspace(-0.25, 0.25, 7) r = xr.DataArray( - data=1 + w[:, np.newaxis] * np.cos(theta), - coords=[("w", w), ("theta", theta)], + data=1 + w[:, np.newaxis] * np.cos(theta), coords=[("w", w), ("theta", theta)], ) x = r * np.cos(theta) y = r * np.sin(theta) z = xr.DataArray( - data=w[:, np.newaxis] * np.sin(theta), - coords=[("w", w), ("theta", theta)], + data=w[:, np.newaxis] * np.sin(theta), coords=[("w", w), ("theta", theta)], ) kwargs = {"fill_value": None} @@ -875,10 +873,7 @@ def test_interpolate_chunk_advanced(method): @requires_scipy def test_interp1d_bounds_error(): """Ensure exception on bounds error is raised if requested""" - da = xr.DataArray( - np.sin(0.3 * np.arange(4)), - [("time", np.arange(4))], - ) + da = xr.DataArray(np.sin(0.3 * np.arange(4)), [("time", np.arange(4))],) with pytest.raises(ValueError): da.interp(time=3.5, kwargs=dict(bounds_error=True)) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 847c4aad0bf..ce31a46946e 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -270,10 +270,7 @@ def test_line_plot_along_1d_coord(self): assert_array_equal(line.get_ydata(), da.coords["time"].values) def test_line_plot_wrong_hue(self): - da = xr.DataArray( - data=np.array([[0, 1], [5, 9]]), - dims=["x", "t"], - ) + da = xr.DataArray(data=np.array([[0, 1], [5, 9]]), dims=["x", "t"],) with pytest.raises(ValueError, match="hue must be one of"): da.plot(x="t", hue="wrong_coord") diff --git a/xarray/tests/test_plugins.py b/xarray/tests/test_plugins.py index b35971e185b..1aff3e6f262 100644 --- a/xarray/tests/test_plugins.py +++ b/xarray/tests/test_plugins.py @@ -136,12 +136,8 @@ def test_build_engines(): ) def test_build_engines_sorted(): dummy_pkg_entrypoints = [ - pkg_resources.EntryPoint.parse( - "dummy2 = xarray.tests.test_plugins:backend_1", - ), - pkg_resources.EntryPoint.parse( - "dummy1 = xarray.tests.test_plugins:backend_1", - ), + pkg_resources.EntryPoint.parse("dummy2 = xarray.tests.test_plugins:backend_1",), + pkg_resources.EntryPoint.parse("dummy1 = xarray.tests.test_plugins:backend_1",), ] backend_entrypoints = plugins.build_engines(dummy_pkg_entrypoints) backend_entrypoints = list(backend_entrypoints) @@ -171,8 +167,7 @@ def test_no_matching_engine_found(): @mock.patch( - "xarray.backends.plugins.list_engines", - mock.MagicMock(return_value={}), + "xarray.backends.plugins.list_engines", mock.MagicMock(return_value={}), ) def test_no_engines_installed(): with pytest.raises(ValueError, match="no currently installed IO backends."): diff --git a/xarray/tests/test_testing.py b/xarray/tests/test_testing.py index dc1db4dc8d7..93aa9d63a8d 100644 --- a/xarray/tests/test_testing.py +++ b/xarray/tests/test_testing.py @@ -95,10 +95,7 @@ def test_assert_duckarray_equal_failing(duckarray, obj1, obj2): @pytest.mark.parametrize( "duckarray", ( - pytest.param( - np.array, - id="numpy", - ), + pytest.param(np.array, id="numpy",), pytest.param( dask_from_array, id="dask", diff --git a/xarray/tests/test_units.py b/xarray/tests/test_units.py index cc3c1a292ec..3091811f249 100644 --- a/xarray/tests/test_units.py +++ b/xarray/tests/test_units.py @@ -620,9 +620,7 @@ def test_align_dataset(value, unit, variant, error, dtype): units_a = extract_units(ds1) units_b = extract_units(ds2) expected_a, expected_b = func( - strip_units(ds1), - strip_units(convert_units(ds2, units_a)), - **stripped_kwargs, + strip_units(ds1), strip_units(convert_units(ds2, units_a)), **stripped_kwargs, ) expected_a = attach_units(expected_a, units_a) if isinstance(array2, Quantity): @@ -1733,10 +1731,7 @@ def test_missing_value_fillna(self, unit, error): pytest.param(1, id="no_unit"), pytest.param(unit_registry.dimensionless, id="dimensionless"), pytest.param(unit_registry.s, id="incompatible_unit"), - pytest.param( - unit_registry.cm, - id="compatible_unit", - ), + pytest.param(unit_registry.cm, id="compatible_unit",), pytest.param(unit_registry.m, id="identical_unit"), ), ) @@ -2182,8 +2177,7 @@ def test_pad(self, mode, xr_arg, np_arg): v = xr.Variable(["x", "y", "z"], data) expected = attach_units( - strip_units(v).pad(mode=mode, **xr_arg), - extract_units(v), + strip_units(v).pad(mode=mode, **xr_arg), extract_units(v), ) actual = v.pad(mode=mode, **xr_arg) @@ -2413,10 +2407,7 @@ def test_binary_operations(self, func, dtype): ( pytest.param(operator.lt, id="less_than"), pytest.param(operator.ge, id="greater_equal"), - pytest.param( - operator.eq, - id="equal", - ), + pytest.param(operator.eq, id="equal",), ), ) @pytest.mark.parametrize( @@ -2908,16 +2899,8 @@ def test_interpolate_na(self): unit_registry.dimensionless, DimensionalityError, id="dimensionless" ), pytest.param(unit_registry.s, DimensionalityError, id="incompatible_unit"), - pytest.param( - unit_registry.cm, - None, - id="compatible_unit", - ), - pytest.param( - unit_registry.m, - None, - id="identical_unit", - ), + pytest.param(unit_registry.cm, None, id="compatible_unit",), + pytest.param(unit_registry.m, None, id="identical_unit",), ), ) def test_combine_first(self, unit, error, dtype): @@ -3469,9 +3452,7 @@ def test_interp_reindex(self, variant, func, dtype): ), ) @pytest.mark.parametrize( - "func", - (method("interp"), method("reindex")), - ids=repr, + "func", (method("interp"), method("reindex")), ids=repr, ) def test_interp_reindex_indexing(self, func, unit, error, dtype): array = np.linspace(1, 2, 10).astype(dtype) @@ -3545,9 +3526,7 @@ def test_interp_reindex_like(self, variant, func, dtype): ), ) @pytest.mark.parametrize( - "func", - (method("interp_like"), method("reindex_like")), - ids=repr, + "func", (method("interp_like"), method("reindex_like")), ids=repr, ) def test_interp_reindex_like_indexing(self, func, unit, error, dtype): array = np.linspace(1, 2, 10).astype(dtype) @@ -3933,8 +3912,7 @@ def test_init(self, shared, unit, error, dtype): ( "data", pytest.param( - "dims", - marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", marks=pytest.mark.skip(reason="indexes don't support units"), ), "coords", ), @@ -4170,11 +4148,7 @@ def test_missing_value_filling(self, func, dtype): unit_registry.dimensionless, DimensionalityError, id="dimensionless" ), pytest.param(unit_registry.s, DimensionalityError, id="incompatible_unit"), - pytest.param( - unit_registry.cm, - None, - id="compatible_unit", - ), + pytest.param(unit_registry.cm, None, id="compatible_unit",), pytest.param(unit_registry.m, None, id="identical_unit"), ), ) @@ -4319,10 +4293,7 @@ def test_where(self, variant, unit, error, dtype): for key, value in kwargs.items() } - expected = attach_units( - strip_units(ds).where(**kwargs_without_units), - units, - ) + expected = attach_units(strip_units(ds).where(**kwargs_without_units), units,) actual = ds.where(**kwargs) assert_units_equal(expected, actual) @@ -4341,10 +4312,7 @@ def test_interpolate_na(self, dtype): ds = xr.Dataset({"a": ("x", array1), "b": ("x", array2)}) units = extract_units(ds) - expected = attach_units( - strip_units(ds).interpolate_na(dim="x"), - units, - ) + expected = attach_units(strip_units(ds).interpolate_na(dim="x"), units,) actual = ds.interpolate_na(dim="x") assert_units_equal(expected, actual) @@ -4367,8 +4335,7 @@ def test_interpolate_na(self, dtype): ( "data", pytest.param( - "dims", - marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4387,8 +4354,7 @@ def test_combine_first(self, variant, unit, error, dtype): ) x = np.arange(len(array1)) * dims_unit ds = xr.Dataset( - data_vars={"a": ("x", array1), "b": ("x", array2)}, - coords={"x": x}, + data_vars={"a": ("x", array1), "b": ("x", array2)}, coords={"x": x}, ) units = extract_units(ds) @@ -4465,8 +4431,7 @@ def test_comparisons(self, func, variant, unit, dtype): y = coord * coord_unit ds = xr.Dataset( - data_vars={"a": ("x", a), "b": ("x", b)}, - coords={"x": x, "y": ("x", y)}, + data_vars={"a": ("x", a), "b": ("x", b)}, coords={"x": x, "y": ("x", y)}, ) units = extract_units(ds) @@ -4523,8 +4488,7 @@ def test_comparisons(self, func, variant, unit, dtype): ( "data", pytest.param( - "dims", - marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4615,8 +4579,7 @@ def test_pad(self, dtype): ( "data", pytest.param( - "dims", - marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4667,10 +4630,7 @@ def test_to_stacked_array(self, dtype): func = method("to_stacked_array", "z", variable_dim="y", sample_dims=["x"]) actual = func(ds).rename(None) - expected = attach_units( - func(strip_units(ds)).rename(None), - units, - ) + expected = attach_units(func(strip_units(ds)).rename(None), units,) assert_units_equal(expected, actual) assert_equal(expected, actual) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 1e0dff45dd2..1a439ab66ed 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -930,8 +930,7 @@ def test_rolling_1d(self): @pytest.mark.parametrize("dims", [("x", "y"), ("y", "z"), ("z", "x")]) def test_nd_rolling(self, center, dims): x = self.cls( - ("x", "y", "z"), - np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), + ("x", "y", "z"), np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), ) window = [3, 3] actual = x.rolling_window( @@ -962,15 +961,11 @@ def test_nd_rolling(self, center, dims): ) def test_rolling_window_errors(self, dim, window, window_dim, center): x = self.cls( - ("x", "y", "z"), - np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), + ("x", "y", "z"), np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), ) with pytest.raises(ValueError): x.rolling_window( - dim=dim, - window=window, - window_dim=window_dim, - center=center, + dim=dim, window=window, window_dim=window_dim, center=center, ) @@ -2046,10 +2041,7 @@ def test_coarsen_keep_attrs(self, operation="mean"): # Test kept attrs with set_options(keep_attrs=True): new = Variable(["coord"], np.linspace(1, 10, 100), attrs=_attrs).coarsen( - windows={"coord": 1}, - func=test_func, - boundary="exact", - side="left", + windows={"coord": 1}, func=test_func, boundary="exact", side="left", ) assert new.attrs == _attrs @@ -2476,8 +2468,7 @@ def test_LazilyIndexedArray(self): self.check_vectorized_indexing(v) # doubly wrapping v = Variable( - dims=("x", "y"), - data=LazilyIndexedArray(LazilyIndexedArray(self.d)), + dims=("x", "y"), data=LazilyIndexedArray(LazilyIndexedArray(self.d)), ) self.check_orthogonal_indexing(v) # hierarchical wrapping diff --git a/xarray/tutorial.py b/xarray/tutorial.py index 78471be7a0e..03f61af7b65 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -73,12 +73,7 @@ def _check_netcdf_engine_installed(name): # idea borrowed from Seaborn def open_dataset( - name, - cache=True, - cache_dir=None, - *, - engine=None, - **kws, + name, cache=True, cache_dir=None, *, engine=None, **kws, ): """ Open a dataset from the online repository (requires internet). @@ -149,11 +144,7 @@ def open_dataset( def open_rasterio( - name, - engine=None, - cache=True, - cache_dir=None, - **kws, + name, engine=None, cache=True, cache_dir=None, **kws, ): """ Open a rasterio dataset from the online repository (requires internet). From 53f56b23dfa2f36f84d8f5b4f883963a7223fa85 Mon Sep 17 00:00:00 2001 From: matthmey Date: Mon, 31 May 2021 14:39:09 +0200 Subject: [PATCH 8/8] proper pre-commit --- .github/workflows/parse_logs.py | 3 +- xarray/backends/api.py | 5 +- xarray/backends/common.py | 5 +- xarray/backends/pydap_.py | 5 +- xarray/backends/pynio_.py | 6 +- xarray/backends/zarr.py | 6 +- xarray/core/accessor_str.py | 159 +++++++++++++---- xarray/core/common.py | 10 +- xarray/core/computation.py | 5 +- xarray/core/dataarray.py | 16 +- xarray/core/dataset.py | 16 +- xarray/core/indexes.py | 6 +- xarray/core/npcompat.py | 6 +- xarray/core/variable.py | 35 +++- xarray/tests/test_accessor_dt.py | 13 +- xarray/tests/test_accessor_str.py | 263 ++++++++++++++++++++-------- xarray/tests/test_backends.py | 27 ++- xarray/tests/test_backends_api.py | 4 +- xarray/tests/test_computation.py | 212 ++++++++++++++++++---- xarray/tests/test_concat.py | 3 +- xarray/tests/test_dataarray.py | 9 +- xarray/tests/test_dataset.py | 24 ++- xarray/tests/test_duck_array_ops.py | 4 +- xarray/tests/test_formatting.py | 5 +- xarray/tests/test_interp.py | 11 +- xarray/tests/test_plot.py | 5 +- xarray/tests/test_plugins.py | 11 +- xarray/tests/test_testing.py | 5 +- xarray/tests/test_units.py | 76 ++++++-- xarray/tests/test_variable.py | 19 +- xarray/tutorial.py | 13 +- 31 files changed, 778 insertions(+), 209 deletions(-) diff --git a/.github/workflows/parse_logs.py b/.github/workflows/parse_logs.py index 0a72d9ba1c2..545beaa4167 100644 --- a/.github/workflows/parse_logs.py +++ b/.github/workflows/parse_logs.py @@ -13,7 +13,8 @@ def extract_short_test_summary_info(lines): up_to_start_of_section = itertools.dropwhile( - lambda l: "=== short test summary info ===" not in l, lines, + lambda l: "=== short test summary info ===" not in l, + lines, ) up_to_section_content = itertools.islice(up_to_start_of_section, 1, None) section_content = itertools.takewhile( diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 027b84ddd7e..e950baed5e0 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -494,7 +494,10 @@ def open_dataset( overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None) backend_ds = backend.open_dataset( - filename_or_obj, drop_variables=drop_variables, **decoders, **kwargs, + filename_or_obj, + drop_variables=drop_variables, + **decoders, + **kwargs, ) ds = _dataset_from_backend_dataset( backend_ds, diff --git a/xarray/backends/common.py b/xarray/backends/common.py index 58468cc25b6..64a245ddead 100644 --- a/xarray/backends/common.py +++ b/xarray/backends/common.py @@ -374,7 +374,10 @@ class BackendEntrypoint: """list of ``open_dataset`` method parameters""" def open_dataset( - self, filename_or_obj: str, drop_variables: Tuple[str] = None, **kwargs: Any, + self, + filename_or_obj: str, + drop_variables: Tuple[str] = None, + **kwargs: Any, ): """ Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`. diff --git a/xarray/backends/pydap_.py b/xarray/backends/pydap_.py index 9ca5af66d18..25d2df9d76a 100644 --- a/xarray/backends/pydap_.py +++ b/xarray/backends/pydap_.py @@ -134,7 +134,10 @@ def open_dataset( DeprecationWarning, ) - store = PydapDataStore.open(filename_or_obj, session=session,) + store = PydapDataStore.open( + filename_or_obj, + session=session, + ) store_entrypoint = StoreBackendEntrypoint() with close_on_error(store): diff --git a/xarray/backends/pynio_.py b/xarray/backends/pynio_.py index 0a892ad9e35..bb57e0bea81 100644 --- a/xarray/backends/pynio_.py +++ b/xarray/backends/pynio_.py @@ -112,7 +112,11 @@ def open_dataset( mode="r", lock=None, ): - store = NioDataStore(filename_or_obj, mode=mode, lock=lock,) + store = NioDataStore( + filename_or_obj, + mode=mode, + lock=lock, + ) filename_or_obj = _normalize_path(filename_or_obj) store_entrypoint = StoreBackendEntrypoint() diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 69da25e8ad1..72c4e99265d 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -319,7 +319,11 @@ def open_group( if isinstance(store, pathlib.Path): store = os.fspath(store) - open_kwargs = dict(mode=mode, synchronizer=synchronizer, path=group,) + open_kwargs = dict( + mode=mode, + synchronizer=synchronizer, + path=group, + ) if LooseVersion(zarr.__version__) >= "2.5.0": open_kwargs["storage_options"] = storage_options elif storage_options: diff --git a/xarray/core/accessor_str.py b/xarray/core/accessor_str.py index d711a4a7b5c..d50163c435b 100644 --- a/xarray/core/accessor_str.py +++ b/xarray/core/accessor_str.py @@ -207,7 +207,10 @@ class StringAccessor: def __init__(self, obj): self._obj = obj - def _stringify(self, invar: Any,) -> Union[str, bytes, Any]: + def _stringify( + self, + invar: Any, + ) -> Union[str, bytes, Any]: """ Convert a string-like to the correct string/bytes type. @@ -285,19 +288,31 @@ def len(self) -> Any: """ return self._apply(func=len, dtype=int) - def __getitem__(self, key: Union[int, slice],) -> Any: + def __getitem__( + self, + key: Union[int, slice], + ) -> Any: if isinstance(key, slice): return self.slice(start=key.start, stop=key.stop, step=key.step) else: return self.get(key) - def __add__(self, other: Any,) -> Any: + def __add__( + self, + other: Any, + ) -> Any: return self.cat(other, sep="") - def __mul__(self, num: Union[int, Any],) -> Any: + def __mul__( + self, + num: Union[int, Any], + ) -> Any: return self.repeat(num) - def __mod__(self, other: Any,) -> Any: + def __mod__( + self, + other: Any, + ) -> Any: if isinstance(other, dict): other = {key: self._stringify(val) for key, val in other.items()} return self._apply(func=lambda x: x % other) @@ -307,7 +322,11 @@ def __mod__(self, other: Any,) -> Any: else: return self._apply(func=lambda x, y: x % y, func_args=(other,)) - def get(self, i: Union[int, Any], default: Union[str, bytes] = "",) -> Any: + def get( + self, + i: Union[int, Any], + default: Union[str, bytes] = "", + ) -> Any: """ Extract character number `i` from each string in the array. @@ -414,7 +433,11 @@ def func(x, istart, istop, irepl): return self._apply(func=func, func_args=(start, stop, repl)) - def cat(self, *others, sep: Union[str, bytes, Any] = "",) -> Any: + def cat( + self, + *others, + sep: Union[str, bytes, Any] = "", + ) -> Any: """ Concatenate strings elementwise in the DataArray with other strings. @@ -489,9 +512,17 @@ def cat(self, *others, sep: Union[str, bytes, Any] = "",) -> Any: # sep will go at the end of the input arguments. func = lambda *x: x[-1].join(x[:-1]) - return self._apply(func=func, func_args=others, dtype=self._obj.dtype.kind,) + return self._apply( + func=func, + func_args=others, + dtype=self._obj.dtype.kind, + ) - def join(self, dim: Hashable = None, sep: Union[str, bytes, Any] = "",) -> Any: + def join( + self, + dim: Hashable = None, + sep: Union[str, bytes, Any] = "", + ) -> Any: """ Concatenate strings in a DataArray along a particular dimension. @@ -558,7 +589,11 @@ def join(self, dim: Hashable = None, sep: Union[str, bytes, Any] = "",) -> Any: # concatenate the resulting arrays return start.str.cat(*others, sep=sep) - def format(self, *args: Any, **kwargs: Any,) -> Any: + def format( + self, + *args: Any, + **kwargs: Any, + ) -> Any: """ Perform python string formatting on each element of the DataArray. @@ -703,7 +738,10 @@ def casefold(self) -> Any: """ return self._apply(func=lambda x: x.casefold()) - def normalize(self, form: str,) -> Any: + def normalize( + self, + form: str, + ) -> Any: """ Return the Unicode normal form for the strings in the datarray. @@ -822,7 +860,10 @@ def isupper(self) -> Any: return self._apply(func=lambda x: x.isupper(), dtype=bool) def count( - self, pat: Union[str, bytes, Pattern, Any], flags: int = 0, case: bool = None, + self, + pat: Union[str, bytes, Pattern, Any], + flags: int = 0, + case: bool = None, ) -> Any: """ Count occurrences of pattern in each string of the array. @@ -859,7 +900,10 @@ def count( func = lambda x, ipat: len(ipat.findall(x)) return self._apply(func=func, func_args=(pat,), dtype=int) - def startswith(self, pat: Union[str, bytes, Any],) -> Any: + def startswith( + self, + pat: Union[str, bytes, Any], + ) -> Any: """ Test if the start of each string in the array matches a pattern. @@ -882,7 +926,10 @@ def startswith(self, pat: Union[str, bytes, Any],) -> Any: func = lambda x, y: x.startswith(y) return self._apply(func=func, func_args=(pat,), dtype=bool) - def endswith(self, pat: Union[str, bytes, Any],) -> Any: + def endswith( + self, + pat: Union[str, bytes, Any], + ) -> Any: """ Test if the end of each string in the array matches a pattern. @@ -965,7 +1012,9 @@ def overfunc(x, iwidth, ifillchar): return self._apply(func=overfunc, func_args=(width, fillchar)) def center( - self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", + self, + width: Union[int, Any], + fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad left and right side of each string in the array. @@ -990,7 +1039,9 @@ def center( return self._padder(func=func, width=width, fillchar=fillchar) def ljust( - self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", + self, + width: Union[int, Any], + fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad right side of each string in the array. @@ -1015,7 +1066,9 @@ def ljust( return self._padder(func=func, width=width, fillchar=fillchar) def rjust( - self, width: Union[int, Any], fillchar: Union[str, bytes, Any] = " ", + self, + width: Union[int, Any], + fillchar: Union[str, bytes, Any] = " ", ) -> Any: """ Pad left side of each string in the array. @@ -1136,7 +1189,10 @@ def func(x, ipat): return self._apply(func=func, func_args=(pat,), dtype=bool) def match( - self, pat: Union[str, bytes, Pattern, Any], case: bool = None, flags: int = 0, + self, + pat: Union[str, bytes, Pattern, Any], + case: bool = None, + flags: int = 0, ) -> Any: """ Determine if each string in the array matches a regular expression. @@ -1170,7 +1226,9 @@ def match( return self._apply(func=func, func_args=(pat,), dtype=bool) def strip( - self, to_strip: Union[str, bytes, Any] = None, side: str = "both", + self, + to_strip: Union[str, bytes, Any] = None, + side: str = "both", ) -> Any: """ Remove leading and trailing characters. @@ -1208,7 +1266,10 @@ def strip( return self._apply(func=func, func_args=(to_strip,)) - def lstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: + def lstrip( + self, + to_strip: Union[str, bytes, Any] = None, + ) -> Any: """ Remove leading characters. @@ -1231,7 +1292,10 @@ def lstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: """ return self.strip(to_strip, side="left") - def rstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: + def rstrip( + self, + to_strip: Union[str, bytes, Any] = None, + ) -> Any: """ Remove trailing characters. @@ -1254,7 +1318,11 @@ def rstrip(self, to_strip: Union[str, bytes, Any] = None,) -> Any: """ return self.strip(to_strip, side="right") - def wrap(self, width: Union[int, Any], **kwargs,) -> Any: + def wrap( + self, + width: Union[int, Any], + **kwargs, + ) -> Any: """ Wrap long strings in the array in paragraphs with length less than `width`. @@ -1281,7 +1349,10 @@ def wrap(self, width: Union[int, Any], **kwargs,) -> Any: func = lambda x, itw: "\n".join(itw.wrap(x)) return self._apply(func=func, func_args=(tw,)) - def translate(self, table: Mapping[Union[str, bytes], Union[str, bytes]],) -> Any: + def translate( + self, + table: Mapping[Union[str, bytes], Union[str, bytes]], + ) -> Any: """ Map characters of each string through the given mapping table. @@ -1300,7 +1371,10 @@ def translate(self, table: Mapping[Union[str, bytes], Union[str, bytes]],) -> An func = lambda x: x.translate(table) return self._apply(func=func) - def repeat(self, repeats: Union[int, Any],) -> Any: + def repeat( + self, + repeats: Union[int, Any], + ) -> Any: """ Repeat each string in the array. @@ -1880,7 +1954,10 @@ def _get_res(val, ipat, imaxcount=maxcount, dtype=self._obj.dtype): ).astype(self._obj.dtype.kind) def findall( - self, pat: Union[str, bytes, Pattern, Any], case: bool = None, flags: int = 0, + self, + pat: Union[str, bytes, Pattern, Any], + case: bool = None, + flags: int = 0, ) -> Any: r""" Find all occurrences of pattern or regular expression in the DataArray. @@ -1970,7 +2047,11 @@ def func(x, ipat): return self._apply(func=func, func_args=(pat,), dtype=np.object_) def _partitioner( - self, *, func: Callable, dim: Hashable, sep: Optional[Union[str, bytes, Any]], + self, + *, + func: Callable, + dim: Hashable, + sep: Optional[Union[str, bytes, Any]], ) -> Any: """ Implements logic for `partition` and `rpartition`. @@ -1998,7 +2079,9 @@ def _partitioner( ).astype(self._obj.dtype.kind) def partition( - self, dim: Optional[Hashable], sep: Union[str, bytes, Any] = " ", + self, + dim: Optional[Hashable], + sep: Union[str, bytes, Any] = " ", ) -> Any: """ Split the strings in the DataArray at the first occurrence of separator `sep`. @@ -2034,7 +2117,9 @@ def partition( return self._partitioner(func=self._obj.dtype.type.partition, dim=dim, sep=sep) def rpartition( - self, dim: Optional[Hashable], sep: Union[str, bytes, Any] = " ", + self, + dim: Optional[Hashable], + sep: Union[str, bytes, Any] = " ", ) -> Any: """ Split the strings in the DataArray at the last occurrence of separator `sep`. @@ -2352,7 +2437,11 @@ def rsplit( maxsplit=maxsplit, ) - def get_dummies(self, dim: Hashable, sep: Union[str, bytes, Any] = "|",) -> Any: + def get_dummies( + self, + dim: Hashable, + sep: Union[str, bytes, Any] = "|", + ) -> Any: """ Return DataArray of dummy/indicator variables. @@ -2427,7 +2516,11 @@ def get_dummies(self, dim: Hashable, sep: Union[str, bytes, Any] = "|",) -> Any: res.coords[dim] = vals return res - def decode(self, encoding: str, errors: str = "strict",) -> Any: + def decode( + self, + encoding: str, + errors: str = "strict", + ) -> Any: """ Decode character string in the array using indicated encoding. @@ -2453,7 +2546,11 @@ def decode(self, encoding: str, errors: str = "strict",) -> Any: func = lambda x: decoder(x, errors)[0] return self._apply(func=func, dtype=np.str_) - def encode(self, encoding: str, errors: str = "strict",) -> Any: + def encode( + self, + encoding: str, + errors: str = "strict", + ) -> Any: """ Encode character string in the array using indicated encoding. diff --git a/xarray/core/common.py b/xarray/core/common.py index 00148a755e5..e37d0b4b15f 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -1350,7 +1350,10 @@ def isnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.isnull, self, dask="allowed", keep_attrs=keep_attrs, + duck_array_ops.isnull, + self, + dask="allowed", + keep_attrs=keep_attrs, ) def notnull(self, keep_attrs: bool = None): @@ -1383,7 +1386,10 @@ def notnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.notnull, self, dask="allowed", keep_attrs=keep_attrs, + duck_array_ops.notnull, + self, + dask="allowed", + keep_attrs=keep_attrs, ) def isin(self, test_elements): diff --git a/xarray/core/computation.py b/xarray/core/computation.py index e25c34fd794..07b7bca27c5 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -752,7 +752,10 @@ def func(*arrays): ) objs = _all_of_type(args, Variable) - attrs = merge_attrs([obj.attrs for obj in objs], combine_attrs=keep_attrs,) + attrs = merge_attrs( + [obj.attrs for obj in objs], + combine_attrs=keep_attrs, + ) output = [] for dims, data in zip(output_dims, result_data): diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 9c452c88a5b..2ab60b894e1 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1949,7 +1949,9 @@ def set_index( return self._from_temp_dataset(ds) def reset_index( - self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, + self, + dims_or_levels: Union[Hashable, Sequence[Hashable]], + drop: bool = False, ) -> Optional["DataArray"]: """Reset the specified index(es) or multi-index level(s). @@ -3001,7 +3003,10 @@ def _unary_op(self, f: Callable, *args, **kwargs): return da def _binary_op( - self, other, f: Callable, reflexive: bool = False, + self, + other, + f: Callable, + reflexive: bool = False, ): if isinstance(other, (Dataset, groupby.GroupBy)): return NotImplemented @@ -4567,7 +4572,12 @@ def curvefit( ) def drop_duplicates( - self, dim: Hashable, keep: Union[str, bool,] = "first", + self, + dim: Hashable, + keep: Union[ + str, + bool, + ] = "first", ): """Returns a new DataArray with duplicate dimension values removed. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4b6bb51bb7c..1697a7c67aa 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -3297,7 +3297,9 @@ def _rename_all(self, name_dict, dims_dict): return variables, coord_names, dims, indexes def rename( - self, name_dict: Mapping[Hashable, Hashable] = None, **names: Hashable, + self, + name_dict: Mapping[Hashable, Hashable] = None, + **names: Hashable, ) -> "Dataset": """Returns a new object with renamed variables and dimensions. @@ -3719,7 +3721,9 @@ def set_index( return self._replace_vars_and_dims(variables, coord_names=coord_names) def reset_index( - self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, + self, + dims_or_levels: Union[Hashable, Sequence[Hashable]], + drop: bool = False, ) -> "Dataset": """Reset the specified index(es) or multi-index level(s). @@ -6248,7 +6252,9 @@ def differentiate(self, coord, edge_order=1, datetime_unit=None): return self._replace(variables) def integrate( - self, coord: Union[Hashable, Sequence[Hashable]], datetime_unit: str = None, + self, + coord: Union[Hashable, Sequence[Hashable]], + datetime_unit: str = None, ) -> "Dataset": """Integrate along the given coordinate using the trapezoidal rule. @@ -6362,7 +6368,9 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False): ) def cumulative_integrate( - self, coord: Union[Hashable, Sequence[Hashable]], datetime_unit: str = None, + self, + coord: Union[Hashable, Sequence[Hashable]], + datetime_unit: str = None, ) -> "Dataset": """Integrate along the given coordinate using the trapezoidal rule. diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 4fa94595c20..be362e1c942 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -136,7 +136,11 @@ def intersection(self, other): def __getitem__( self, indexer ) -> Union[ - "PandasIndex", NumpyIndexingAdapter, np.ndarray, np.datetime64, np.timedelta64, + "PandasIndex", + NumpyIndexingAdapter, + np.ndarray, + np.datetime64, + np.timedelta64, ]: key = indexer.tuple if isinstance(key, tuple) and len(key) == 1: diff --git a/xarray/core/npcompat.py b/xarray/core/npcompat.py index c8880c426bf..35bac982d4c 100644 --- a/xarray/core/npcompat.py +++ b/xarray/core/npcompat.py @@ -112,10 +112,12 @@ def __array__(self) -> np.ndarray: ] _RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]] _ArrayLike = Union[ - _NestedSequence[_SupportsArray], _NestedSequence[_T], + _NestedSequence[_SupportsArray], + _NestedSequence[_T], ] _ArrayLikeFallback = Union[ - _ArrayLike[Union[bool, int, float, complex, str, bytes]], _RecursiveSequence, + _ArrayLike[Union[bool, int, float, complex, str, bytes]], + _RecursiveSequence, ] # The extra step defining _ArrayLikeFallback and using ArrayLike as a type # alias for it works around an issue with mypy. diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 418c613659e..1abaad49cdf 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -48,7 +48,12 @@ ) NON_NUMPY_SUPPORTED_ARRAY_TYPES = ( - (indexing.ExplicitlyIndexed, pd.Index,) + dask_array_type + cupy_array_type + ( + indexing.ExplicitlyIndexed, + pd.Index, + ) + + dask_array_type + + cupy_array_type ) # https://github.com/python/mypy/issues/224 BASIC_INDEXING_TYPES = integer_types + (slice,) @@ -1556,7 +1561,10 @@ def _unstack_once_full( return Variable(new_dims, new_data, self._attrs, self._encoding, fastpath=True) def _unstack_once( - self, index: pd.MultiIndex, dim: Hashable, fill_value=dtypes.NA, + self, + index: pd.MultiIndex, + dim: Hashable, + fill_value=dtypes.NA, ) -> "Variable": """ Unstacks this variable given an index to unstack and the name of the @@ -1585,7 +1593,10 @@ def _unstack_once( dtype = self.dtype data = np.full_like( - self.data, fill_value=fill_value, shape=new_shape, dtype=dtype, + self.data, + fill_value=fill_value, + shape=new_shape, + dtype=dtype, ) # Indexer is a list of lists of locations. Each list is the locations @@ -1657,7 +1668,13 @@ def clip(self, min=None, max=None): return apply_ufunc(np.clip, self, min, max, dask="allowed") def reduce( - self, func, dim=None, axis=None, keep_attrs=None, keepdims=False, **kwargs, + self, + func, + dim=None, + axis=None, + keep_attrs=None, + keepdims=False, + **kwargs, ): """Reduce this array by applying `func` along some dimension(s). @@ -2243,7 +2260,10 @@ def isnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.isnull, self, dask="allowed", keep_attrs=keep_attrs, + duck_array_ops.isnull, + self, + dask="allowed", + keep_attrs=keep_attrs, ) def notnull(self, keep_attrs: bool = None): @@ -2274,7 +2294,10 @@ def notnull(self, keep_attrs: bool = None): keep_attrs = _get_keep_attrs(default=False) return apply_ufunc( - duck_array_ops.notnull, self, dask="allowed", keep_attrs=keep_attrs, + duck_array_ops.notnull, + self, + dask="allowed", + keep_attrs=keep_attrs, ) @property diff --git a/xarray/tests/test_accessor_dt.py b/xarray/tests/test_accessor_dt.py index ce9c7e3451c..62da3bab2cd 100644 --- a/xarray/tests/test_accessor_dt.py +++ b/xarray/tests/test_accessor_dt.py @@ -90,7 +90,11 @@ def test_field_access(self, field): @pytest.mark.parametrize( "field, pandas_field", - [("year", "year"), ("week", "week"), ("weekday", "day"),], + [ + ("year", "year"), + ("week", "week"), + ("weekday", "day"), + ], ) def test_isocalendar(self, field, pandas_field): @@ -171,7 +175,12 @@ def test_dask_field_access(self, field): @requires_dask @pytest.mark.parametrize( - "field", ["year", "week", "weekday",], + "field", + [ + "year", + "week", + "weekday", + ], ) def test_isocalendar_dask(self, field): import dask.array as da diff --git a/xarray/tests/test_accessor_str.py b/xarray/tests/test_accessor_str.py index 31ca7388a43..519ca762c41 100644 --- a/xarray/tests/test_accessor_str.py +++ b/xarray/tests/test_accessor_str.py @@ -834,11 +834,15 @@ def test_extract_multi_nocase(dtype): def test_extract_broadcast(dtype): - value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) + value = xr.DataArray( + ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], + dims=["X"], + ).astype(dtype) - pat_str = xr.DataArray([r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], dims=["Y"],).astype( - dtype - ) + pat_str = xr.DataArray( + [r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], + dims=["Y"], + ).astype(dtype) pat_re = value.str._re_compile(pat=pat_str) expected = [ @@ -1161,11 +1165,15 @@ def test_extractall_multi_multi_nocase(dtype): def test_extractall_broadcast(dtype): - value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) + value = xr.DataArray( + ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], + dims=["X"], + ).astype(dtype) - pat_str = xr.DataArray([r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], dims=["Y"],).astype( - dtype - ) + pat_str = xr.DataArray( + [r"(\w+)_Xy_(\d*)", r"(\w+)_xY_(\d*)"], + dims=["Y"], + ).astype(dtype) pat_re = value.str._re_compile(pat=pat_str) expected = [ @@ -1254,7 +1262,11 @@ def test_findall_single_multi_case(dtype): expected = [ [["a"], ["bab", "baab"], ["abc", "cbc"]], - [["abcd", "dcd", "dccd"], [], ["abcdef", "fef"],], + [ + ["abcd", "dcd", "dccd"], + [], + ["abcdef", "fef"], + ], ] expected = [[[dtype(x) for x in y] for y in z] for z in expected] expected = np.array(expected, dtype=np.object_) @@ -1290,8 +1302,16 @@ def test_findall_single_multi_nocase(dtype): ).astype(dtype) expected = [ - [["a"], ["ab", "bab", "baab"], ["abc", "cbc"],], - [["abcd", "dcd", "dccd"], [], ["abcdef", "fef"],], + [ + ["a"], + ["ab", "bab", "baab"], + ["abc", "cbc"], + ], + [ + ["abcd", "dcd", "dccd"], + [], + ["abcdef", "fef"], + ], ] expected = [[[dtype(x) for x in y] for y in z] for z in expected] expected = np.array(expected, dtype=np.object_) @@ -1452,11 +1472,15 @@ def test_findall_multi_multi_nocase(dtype): def test_findall_broadcast(dtype): - value = xr.DataArray(["a_Xy_0", "ab_xY_10", "abc_Xy_01"], dims=["X"],).astype(dtype) + value = xr.DataArray( + ["a_Xy_0", "ab_xY_10", "abc_Xy_01"], + dims=["X"], + ).astype(dtype) - pat_str = xr.DataArray([r"(\w+)_Xy_\d*", r"\w+_Xy_(\d*)"], dims=["Y"],).astype( - dtype - ) + pat_str = xr.DataArray( + [r"(\w+)_Xy_\d*", r"\w+_Xy_(\d*)"], + dims=["Y"], + ).astype(dtype) pat_re = value.str._re_compile(pat=pat_str) expected = [[["a"], ["0"]], [[], []], [["abc"], ["01"]]] @@ -2405,7 +2429,11 @@ def test_partition_whitespace(dtype): ).astype(dtype) exp_part_dim = [ - [["abc", " ", "def"], ["spam", " ", "eggs swallow"], ["red_blue", "", ""],], + [ + ["abc", " ", "def"], + ["spam", " ", "eggs swallow"], + ["red_blue", "", ""], + ], [ ["test0", " ", "test1 test2 test3"], ["", "", ""], @@ -2414,7 +2442,11 @@ def test_partition_whitespace(dtype): ] exp_rpart_dim = [ - [["abc", " ", "def"], ["spam eggs", " ", "swallow"], ["", "", "red_blue"],], + [ + ["abc", " ", "def"], + ["spam eggs", " ", "swallow"], + ["", "", "red_blue"], + ], [ ["test0 test1 test2", " ", "test3"], ["", "", ""], @@ -2445,7 +2477,11 @@ def test_partition_comma(dtype): ).astype(dtype) exp_part_dim = [ - [["abc", ", ", "def"], ["spam", ", ", "eggs, swallow"], ["red_blue", "", ""],], + [ + ["abc", ", ", "def"], + ["spam", ", ", "eggs, swallow"], + ["red_blue", "", ""], + ], [ ["test0", ", ", "test1, test2, test3"], ["", "", ""], @@ -2454,7 +2490,11 @@ def test_partition_comma(dtype): ] exp_rpart_dim = [ - [["abc", ", ", "def"], ["spam, eggs", ", ", "swallow"], ["", "", "red_blue"],], + [ + ["abc", ", ", "def"], + ["spam, eggs", ", ", "swallow"], + ["", "", "red_blue"], + ], [ ["test0, test1, test2", ", ", "test3"], ["", "", ""], @@ -2763,10 +2803,14 @@ def test_split_comma(dtype): def test_splitters_broadcast(dtype): values = xr.DataArray( - ["ab cd,de fg", "spam, ,eggs swallow", "red_blue"], dims=["X"], + ["ab cd,de fg", "spam, ,eggs swallow", "red_blue"], + dims=["X"], ).astype(dtype) - sep = xr.DataArray([" ", ","], dims=["Y"],).astype(dtype) + sep = xr.DataArray( + [" ", ","], + dims=["Y"], + ).astype(dtype) expected_left = xr.DataArray( [ @@ -2876,9 +2920,15 @@ def test_get_dummies(dtype): def test_get_dummies_broadcast(dtype): - values = xr.DataArray(["x~x|x~x", "x", "x|x~x", "x~x"], dims=["X"],).astype(dtype) + values = xr.DataArray( + ["x~x|x~x", "x", "x|x~x", "x~x"], + dims=["X"], + ).astype(dtype) - sep = xr.DataArray(["|", "~"], dims=["Y"],).astype(dtype) + sep = xr.DataArray( + ["|", "~"], + dims=["Y"], + ).astype(dtype) expected = [ [[False, False, True], [True, True, False]], @@ -2908,7 +2958,10 @@ def test_get_dummies_empty(dtype): def test_splitters_empty_str(dtype): - values = xr.DataArray([["", "", ""], ["", "", ""]], dims=["X", "Y"],).astype(dtype) + values = xr.DataArray( + [["", "", ""], ["", "", ""]], + dims=["X", "Y"], + ).astype(dtype) targ_partition_dim = xr.DataArray( [ @@ -2927,13 +2980,18 @@ def test_splitters_empty_str(dtype): ] targ_partition_none = np.array(targ_partition_none, dtype=np.object_) del targ_partition_none[-1, -1][-1] - targ_partition_none = xr.DataArray(targ_partition_none, dims=["X", "Y"],) + targ_partition_none = xr.DataArray( + targ_partition_none, + dims=["X", "Y"], + ) targ_split_dim = xr.DataArray( - [[[""], [""], [""]], [[""], [""], [""]]], dims=["X", "Y", "ZZ"], + [[[""], [""], [""]], [[""], [""], [""]]], + dims=["X", "Y", "ZZ"], ).astype(dtype) targ_split_none = xr.DataArray( - np.array([[[], [], []], [[], [], [""]]], dtype=np.object_), dims=["X", "Y"], + np.array([[[], [], []], [[], [], [""]]], dtype=np.object_), + dims=["X", "Y"], ) del targ_split_none.data[-1, -1][-1] @@ -2976,7 +3034,8 @@ def test_splitters_empty_str(dtype): def test_cat_str(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], + dims=["X", "Y"], ).astype(dtype) values_2 = "111" @@ -3021,10 +3080,12 @@ def test_cat_str(dtype): def test_cat_uniform(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], + dims=["X", "Y"], ).astype(dtype) values_2 = xr.DataArray( - [["11111", "222", "33"], ["4", "5555", "66"]], dims=["X", "Y"], + [["11111", "222", "33"], ["4", "5555", "66"]], + dims=["X", "Y"], ) targ_blank = xr.DataArray( @@ -3068,9 +3129,13 @@ def test_cat_uniform(dtype): def test_cat_broadcast_right(dtype): values_1 = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], + dims=["X", "Y"], ).astype(dtype) - values_2 = xr.DataArray(["11111", "222", "33"], dims=["Y"],) + values_2 = xr.DataArray( + ["11111", "222", "33"], + dims=["Y"], + ) targ_blank = xr.DataArray( [["a11111", "bb222", "cccc33"], ["ddddd11111", "eeee222", "fff33"]], @@ -3112,9 +3177,13 @@ def test_cat_broadcast_right(dtype): def test_cat_broadcast_left(dtype): - values_1 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) + values_1 = xr.DataArray( + ["a", "bb", "cccc"], + dims=["Y"], + ).astype(dtype) values_2 = xr.DataArray( - [["11111", "222", "33"], ["4", "5555", "66"]], dims=["X", "Y"], + [["11111", "222", "33"], ["4", "5555", "66"]], + dims=["X", "Y"], ) targ_blank = ( @@ -3173,8 +3242,14 @@ def test_cat_broadcast_left(dtype): def test_cat_broadcast_both(dtype): - values_1 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) - values_2 = xr.DataArray(["11111", "4"], dims=["X"],) + values_1 = xr.DataArray( + ["a", "bb", "cccc"], + dims=["Y"], + ).astype(dtype) + values_2 = xr.DataArray( + ["11111", "4"], + dims=["X"], + ) targ_blank = ( xr.DataArray( @@ -3232,9 +3307,15 @@ def test_cat_broadcast_both(dtype): def test_cat_multi(): - values_1 = xr.DataArray(["11111", "4"], dims=["X"],) + values_1 = xr.DataArray( + ["11111", "4"], + dims=["X"], + ) - values_2 = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(np.bytes_) + values_2 = xr.DataArray( + ["a", "bb", "cccc"], + dims=["Y"], + ).astype(np.bytes_) values_3 = np.array(3.4) @@ -3242,7 +3323,10 @@ def test_cat_multi(): values_5 = np.array("", dtype=np.unicode_) - sep = xr.DataArray([" ", ", "], dims=["ZZ"],).astype(np.unicode_) + sep = xr.DataArray( + [" ", ", "], + dims=["ZZ"], + ).astype(np.unicode_) expected = xr.DataArray( [ @@ -3282,7 +3366,10 @@ def test_join_scalar(dtype): def test_join_vector(dtype): - values = xr.DataArray(["a", "bb", "cccc"], dims=["Y"],).astype(dtype) + values = xr.DataArray( + ["a", "bb", "cccc"], + dims=["Y"], + ).astype(dtype) targ_blank = xr.DataArray("abbcccc").astype(dtype) targ_space = xr.DataArray("a bb cccc").astype(dtype) @@ -3306,20 +3393,27 @@ def test_join_vector(dtype): def test_join_2d(dtype): values = xr.DataArray( - [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], dims=["X", "Y"], + [["a", "bb", "cccc"], ["ddddd", "eeee", "fff"]], + dims=["X", "Y"], ).astype(dtype) - targ_blank_x = xr.DataArray(["addddd", "bbeeee", "ccccfff"], dims=["Y"],).astype( - dtype - ) - targ_space_x = xr.DataArray(["a ddddd", "bb eeee", "cccc fff"], dims=["Y"],).astype( - dtype - ) + targ_blank_x = xr.DataArray( + ["addddd", "bbeeee", "ccccfff"], + dims=["Y"], + ).astype(dtype) + targ_space_x = xr.DataArray( + ["a ddddd", "bb eeee", "cccc fff"], + dims=["Y"], + ).astype(dtype) - targ_blank_y = xr.DataArray(["abbcccc", "dddddeeeefff"], dims=["X"],).astype(dtype) - targ_space_y = xr.DataArray(["a bb cccc", "ddddd eeee fff"], dims=["X"],).astype( - dtype - ) + targ_blank_y = xr.DataArray( + ["abbcccc", "dddddeeeefff"], + dims=["X"], + ).astype(dtype) + targ_space_y = xr.DataArray( + ["a bb cccc", "ddddd eeee fff"], + dims=["X"], + ).astype(dtype) res_blank_x = values.str.join(dim="X") res_blank_y = values.str.join(dim="Y") @@ -3344,11 +3438,20 @@ def test_join_2d(dtype): def test_join_broadcast(dtype): - values = xr.DataArray(["a", "bb", "cccc"], dims=["X"],).astype(dtype) + values = xr.DataArray( + ["a", "bb", "cccc"], + dims=["X"], + ).astype(dtype) - sep = xr.DataArray([" ", ", "], dims=["ZZ"],).astype(dtype) + sep = xr.DataArray( + [" ", ", "], + dims=["ZZ"], + ).astype(dtype) - expected = xr.DataArray(["a bb cccc", "a, bb, cccc"], dims=["ZZ"],).astype(dtype) + expected = xr.DataArray( + ["a bb cccc", "a, bb, cccc"], + dims=["ZZ"], + ).astype(dtype) res = values.str.join(sep=sep) @@ -3358,7 +3461,8 @@ def test_join_broadcast(dtype): def test_format_scalar(): values = xr.DataArray( - ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], dims=["X"], + ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], + dims=["X"], ).astype(np.unicode_) pos0 = 1 @@ -3370,7 +3474,8 @@ def test_format_scalar(): W = "NO!" expected = xr.DataArray( - ["1.X.None", "1,1.2,'test','test'", "'test'-X-None"], dims=["X"], + ["1.X.None", "1,1.2,'test','test'", "'test'-X-None"], + dims=["X"], ).astype(np.unicode_) res = values.str.format(pos0, pos1, pos2, X=X, Y=Y, ZZ=ZZ, W=W) @@ -3381,13 +3486,17 @@ def test_format_scalar(): def test_format_broadcast(): values = xr.DataArray( - ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], dims=["X"], + ["{}.{Y}.{ZZ}", "{},{},{X},{X}", "{X}-{Y}-{ZZ}"], + dims=["X"], ).astype(np.unicode_) pos0 = 1 pos1 = 1.2 - pos2 = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) + pos2 = xr.DataArray( + ["2.3", "3.44444"], + dims=["YY"], + ) X = "'test'" Y = "X" @@ -3410,16 +3519,18 @@ def test_format_broadcast(): def test_mod_scalar(): - values = xr.DataArray(["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], dims=["X"],).astype( - np.unicode_ - ) + values = xr.DataArray( + ["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], + dims=["X"], + ).astype(np.unicode_) pos0 = 1 pos1 = 1.2 pos2 = "2.3" expected = xr.DataArray( - ["1.1.2.2.3", "1,1.2,2.3", "1-1.2-2.3"], dims=["X"], + ["1.1.2.2.3", "1,1.2,2.3", "1-1.2-2.3"], + dims=["X"], ).astype(np.unicode_) res = values.str % (pos0, pos1, pos2) @@ -3430,7 +3541,8 @@ def test_mod_scalar(): def test_mod_dict(): values = xr.DataArray( - ["%(a)s.%(a)s.%(b)s", "%(b)s,%(c)s,%(b)s", "%(c)s-%(b)s-%(a)s"], dims=["X"], + ["%(a)s.%(a)s.%(b)s", "%(b)s,%(c)s,%(b)s", "%(c)s-%(b)s-%(a)s"], + dims=["X"], ).astype(np.unicode_) a = 1 @@ -3438,7 +3550,8 @@ def test_mod_dict(): c = "2.3" expected = xr.DataArray( - ["1.1.1.2", "1.2,2.3,1.2", "2.3-1.2-1"], dims=["X"], + ["1.1.1.2", "1.2,2.3,1.2", "2.3-1.2-1"], + dims=["X"], ).astype(np.unicode_) res = values.str % {"a": a, "b": b, "c": c} @@ -3448,9 +3561,15 @@ def test_mod_dict(): def test_mod_broadcast_single(): - values = xr.DataArray(["%s_1", "%s_2", "%s_3"], dims=["X"],).astype(np.unicode_) + values = xr.DataArray( + ["%s_1", "%s_2", "%s_3"], + dims=["X"], + ).astype(np.unicode_) - pos = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) + pos = xr.DataArray( + ["2.3", "3.44444"], + dims=["YY"], + ) expected = xr.DataArray( [["2.3_1", "3.44444_1"], ["2.3_2", "3.44444_2"], ["2.3_3", "3.44444_3"]], @@ -3464,14 +3583,18 @@ def test_mod_broadcast_single(): def test_mod_broadcast_multi(): - values = xr.DataArray(["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], dims=["X"],).astype( - np.unicode_ - ) + values = xr.DataArray( + ["%s.%s.%s", "%s,%s,%s", "%s-%s-%s"], + dims=["X"], + ).astype(np.unicode_) pos0 = 1 pos1 = 1.2 - pos2 = xr.DataArray(["2.3", "3.44444"], dims=["YY"],) + pos2 = xr.DataArray( + ["2.3", "3.44444"], + dims=["YY"], + ) expected = xr.DataArray( [ diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index b9adc9df2e0..908d2f4940d 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -879,7 +879,11 @@ def _create_cf_dataset(): np.arange(0.1, 0.9, 0.1).reshape(2, 2, 2), {"standard_name": "standard_error"}, ), - det_lim=((), 0.1, {"standard_name": "detection_minimum"},), + det_lim=( + (), + 0.1, + {"standard_name": "detection_minimum"}, + ), ), dict( latitude=("latitude", [0, 1], {"units": "degrees_north"}), @@ -2822,7 +2826,8 @@ def test_open_fileobj(self): with open(tmp_file, "rb") as f: f.seek(8) with pytest.raises( - ValueError, match="match in any of xarray's currently installed IO", + ValueError, + match="match in any of xarray's currently installed IO", ): with pytest.warns( RuntimeWarning, @@ -5121,7 +5126,14 @@ def test_open_dataset_chunking_zarr(chunks, tmp_path): dask_arr = da.from_array( np.ones((500, 500), dtype="float64"), chunks=encoded_chunks ) - ds = xr.Dataset({"test": xr.DataArray(dask_arr, dims=("x", "y"),)}) + ds = xr.Dataset( + { + "test": xr.DataArray( + dask_arr, + dims=("x", "y"), + ) + } + ) ds["test"].encoding["chunks"] = encoded_chunks ds.to_zarr(tmp_path / "test.zarr") @@ -5144,7 +5156,14 @@ def test_chunking_consintency(chunks, tmp_path): dask_arr = da.from_array( np.ones((500, 500), dtype="float64"), chunks=encoded_chunks ) - ds = xr.Dataset({"test": xr.DataArray(dask_arr, dims=("x", "y"),)}) + ds = xr.Dataset( + { + "test": xr.DataArray( + dask_arr, + dims=("x", "y"), + ) + } + ) ds["test"].encoding["chunks"] = encoded_chunks ds.to_zarr(tmp_path / "test.zarr") ds.to_netcdf(tmp_path / "test.nc") diff --git a/xarray/tests/test_backends_api.py b/xarray/tests/test_backends_api.py index 24cd6548687..4124d0d0b81 100644 --- a/xarray/tests/test_backends_api.py +++ b/xarray/tests/test_backends_api.py @@ -26,7 +26,9 @@ def test_custom_engine(): class CustomBackend(xr.backends.BackendEntrypoint): def open_dataset( - filename_or_obj, drop_variables=None, **kwargs, + filename_or_obj, + drop_variables=None, + **kwargs, ): return expected.copy(deep=True) diff --git a/xarray/tests/test_computation.py b/xarray/tests/test_computation.py index 9826a919390..b7ae1ca9828 100644 --- a/xarray/tests/test_computation.py +++ b/xarray/tests/test_computation.py @@ -476,10 +476,13 @@ def test_unified_dim_sizes(): "x": 1, "y": 2, } - assert unified_dim_sizes( - [xr.Variable(("x", "z"), [[1]]), xr.Variable(("y", "z"), [[1, 2], [3, 4]])], - exclude_dims={"z"}, - ) == {"x": 1, "y": 2} + assert ( + unified_dim_sizes( + [xr.Variable(("x", "z"), [[1]]), xr.Variable(("y", "z"), [[1, 2], [3, 4]])], + exclude_dims={"z"}, + ) + == {"x": 1, "y": 2} + ) # duplicate dimensions with pytest.raises(ValueError): @@ -564,13 +567,41 @@ def add(a, b, keep_attrs): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( - pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), - pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), - pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", + None, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="default", + ), + pytest.param( + False, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="False", + ), + pytest.param( + True, + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="True", + ), + pytest.param( + "override", + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="override", + ), + pytest.param( + "drop", + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="drop", ), - pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -605,13 +636,41 @@ def test_keep_attrs_strategies_variable(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( - pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), - pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), - pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", + None, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="default", + ), + pytest.param( + False, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="False", + ), + pytest.param( + True, + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="True", + ), + pytest.param( + "override", + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="override", + ), + pytest.param( + "drop", + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="drop", ), - pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -647,13 +706,41 @@ def test_keep_attrs_strategies_dataarray(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( - pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), - pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), - pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", + None, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="default", + ), + pytest.param( + False, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="False", + ), + pytest.param( + True, + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="True", + ), + pytest.param( + "override", + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="override", + ), + pytest.param( + "drop", + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="drop", ), - pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -714,13 +801,41 @@ def test_keep_attrs_strategies_dataarray_variables( @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( - pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), - pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), - pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", + None, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="default", + ), + pytest.param( + False, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="False", + ), + pytest.param( + True, + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="True", + ), + pytest.param( + "override", + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="override", + ), + pytest.param( + "drop", + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="drop", ), - pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -756,13 +871,41 @@ def test_keep_attrs_strategies_dataset(strategy, attrs, expected, error): @pytest.mark.parametrize( ["strategy", "attrs", "expected", "error"], ( - pytest.param(None, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="default",), - pytest.param(False, [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="False",), - pytest.param(True, [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="True",), pytest.param( - "override", [{"a": 1}, {"a": 2}, {"a": 3}], {"a": 1}, False, id="override", + None, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="default", + ), + pytest.param( + False, + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="False", + ), + pytest.param( + True, + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="True", + ), + pytest.param( + "override", + [{"a": 1}, {"a": 2}, {"a": 3}], + {"a": 1}, + False, + id="override", + ), + pytest.param( + "drop", + [{"a": 1}, {"a": 2}, {"a": 3}], + {}, + False, + id="drop", ), - pytest.param("drop", [{"a": 1}, {"a": 2}, {"a": 3}], {}, False, id="drop",), pytest.param( "drop_conflicts", [{"a": 1, "b": 2}, {"b": 1, "c": 3}, {"c": 3, "d": 4}], @@ -1153,7 +1296,10 @@ def test_vectorize_dask_dtype_without_output_dtypes(data_array): expected = data_array.copy() actual = apply_ufunc( - identity, data_array.chunk({"x": 1}), vectorize=True, dask="parallelized", + identity, + data_array.chunk({"x": 1}), + vectorize=True, + dask="parallelized", ) assert_identical(expected, actual) @@ -1385,7 +1531,8 @@ def np_corr(ts1, ts2): @pytest.mark.parametrize( - "da_a, da_b", arrays_w_tuples()[1], + "da_a, da_b", + arrays_w_tuples()[1], ) @pytest.mark.parametrize("dim", [None, "time", "x"]) def test_covcorr_consistency(da_a, da_b, dim): @@ -1405,7 +1552,8 @@ def test_covcorr_consistency(da_a, da_b, dim): @pytest.mark.parametrize( - "da_a", arrays_w_tuples()[0], + "da_a", + arrays_w_tuples()[0], ) @pytest.mark.parametrize("dim", [None, "time", "x", ["time", "x"]]) def test_autocov(da_a, dim): diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index 1b59a6bcc34..42232f7df57 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -715,7 +715,8 @@ def test_concat_preserve_coordinate_order(): ) expected = Dataset( - {"data": (["time", "y", "x"], data)}, coords={"time": time, "y": y, "x": x}, + {"data": (["time", "y", "x"], data)}, + coords={"time": time, "y": y, "x": x}, ) actual = concat([ds1, ds2], dim="time") diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 1261e47a6ca..0e10d3816bc 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6501,7 +6501,11 @@ def test_isin(da): @pytest.mark.parametrize( - "funcname, argument", [("reduce", (np.mean,)), ("mean", ()),], + "funcname, argument", + [ + ("reduce", (np.mean,)), + ("mean", ()), + ], ) def test_coarsen_keep_attrs(funcname, argument): attrs_da = {"da_attr": "test"} @@ -6839,7 +6843,8 @@ def test_rolling_stride(center, window, stride): assert_array_equal(actual.isnull(), expected.isnull()) if (~actual.isnull()).sum() > 0: np.allclose( - actual.values, expected.values, + actual.values, + expected.values, ) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 46f1fe1b401..5907790db1f 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -6291,7 +6291,11 @@ def test_coarsen_coords_cftime(): @pytest.mark.parametrize( - "funcname, argument", [("reduce", (np.mean,)), ("mean", ()),], + "funcname, argument", + [ + ("reduce", (np.mean,)), + ("mean", ()), + ], ) def test_coarsen_keep_attrs(funcname, argument): global_attrs = {"units": "test", "long_name": "testing"} @@ -6375,7 +6379,9 @@ def test_coarsen_keep_attrs_deprecated(): coords = np.linspace(1, 10, 100) ds = Dataset( - data_vars={"da": ("coord", data)}, coords={"coord": coords}, attrs=global_attrs, + data_vars={"da": ("coord", data)}, + coords={"coord": coords}, + attrs=global_attrs, ) ds.da.attrs = attrs_da @@ -6503,7 +6509,9 @@ def test_rolling_keep_attrs_deprecated(): coords = np.linspace(1, 10, 100) ds = Dataset( - data_vars={"da": ("coord", data)}, coords={"coord": coords}, attrs=global_attrs, + data_vars={"da": ("coord", data)}, + coords={"coord": coords}, + attrs=global_attrs, ) ds.da.attrs = attrs_da @@ -7064,7 +7072,8 @@ def test_cumulative_integrate(dask): ) assert_allclose(expected_x, actual.compute()) assert_equal( - ds["var"].cumulative_integrate("x"), ds.cumulative_integrate("x")["var"], + ds["var"].cumulative_integrate("x"), + ds.cumulative_integrate("x")["var"], ) # make sure result is also a dask array (if the source is dask array) @@ -7073,12 +7082,15 @@ def test_cumulative_integrate(dask): # along y actual = da.cumulative_integrate("y") expected_y = xr.DataArray( - cumtrapz(da, da["y"], axis=1, initial=0.0), dims=["x", "y"], coords=da.coords, + cumtrapz(da, da["y"], axis=1, initial=0.0), + dims=["x", "y"], + coords=da.coords, ) assert_allclose(expected_y, actual.compute()) assert_equal(actual, ds.cumulative_integrate("y")["var"]) assert_equal( - ds["var"].cumulative_integrate("y"), ds.cumulative_integrate("y")["var"], + ds["var"].cumulative_integrate("y"), + ds.cumulative_integrate("y")["var"], ) # along x and y diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index b2421f32942..0eb007259bb 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -889,6 +889,8 @@ def test_push_dask(): # some chunks of size-1 with NaN with raise_if_dask_computes(): actual = push( - dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)), axis=0, n=None, + dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)), + axis=0, + n=None, ) np.testing.assert_equal(actual, expected) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index d403f7319ae..b9ba57f99dc 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -514,7 +514,10 @@ def test__mapping_repr(display_max_rows, n_vars, n_attr): data_vars = dict() for v in a: data_vars[v] = xr.DataArray( - name=v, data=np.array([3, 4]), dims=["time"], coords=coords, + name=v, + data=np.array([3, 4]), + dims=["time"], + coords=coords, ) ds = xr.Dataset(data_vars) ds.attrs = attrs diff --git a/xarray/tests/test_interp.py b/xarray/tests/test_interp.py index 121aa596759..ab023dc1558 100644 --- a/xarray/tests/test_interp.py +++ b/xarray/tests/test_interp.py @@ -851,13 +851,15 @@ def test_interpolate_chunk_advanced(method): theta = np.linspace(0, 2 * np.pi, 5) w = np.linspace(-0.25, 0.25, 7) r = xr.DataArray( - data=1 + w[:, np.newaxis] * np.cos(theta), coords=[("w", w), ("theta", theta)], + data=1 + w[:, np.newaxis] * np.cos(theta), + coords=[("w", w), ("theta", theta)], ) x = r * np.cos(theta) y = r * np.sin(theta) z = xr.DataArray( - data=w[:, np.newaxis] * np.sin(theta), coords=[("w", w), ("theta", theta)], + data=w[:, np.newaxis] * np.sin(theta), + coords=[("w", w), ("theta", theta)], ) kwargs = {"fill_value": None} @@ -873,7 +875,10 @@ def test_interpolate_chunk_advanced(method): @requires_scipy def test_interp1d_bounds_error(): """Ensure exception on bounds error is raised if requested""" - da = xr.DataArray(np.sin(0.3 * np.arange(4)), [("time", np.arange(4))],) + da = xr.DataArray( + np.sin(0.3 * np.arange(4)), + [("time", np.arange(4))], + ) with pytest.raises(ValueError): da.interp(time=3.5, kwargs=dict(bounds_error=True)) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index ce31a46946e..847c4aad0bf 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -270,7 +270,10 @@ def test_line_plot_along_1d_coord(self): assert_array_equal(line.get_ydata(), da.coords["time"].values) def test_line_plot_wrong_hue(self): - da = xr.DataArray(data=np.array([[0, 1], [5, 9]]), dims=["x", "t"],) + da = xr.DataArray( + data=np.array([[0, 1], [5, 9]]), + dims=["x", "t"], + ) with pytest.raises(ValueError, match="hue must be one of"): da.plot(x="t", hue="wrong_coord") diff --git a/xarray/tests/test_plugins.py b/xarray/tests/test_plugins.py index 1aff3e6f262..b35971e185b 100644 --- a/xarray/tests/test_plugins.py +++ b/xarray/tests/test_plugins.py @@ -136,8 +136,12 @@ def test_build_engines(): ) def test_build_engines_sorted(): dummy_pkg_entrypoints = [ - pkg_resources.EntryPoint.parse("dummy2 = xarray.tests.test_plugins:backend_1",), - pkg_resources.EntryPoint.parse("dummy1 = xarray.tests.test_plugins:backend_1",), + pkg_resources.EntryPoint.parse( + "dummy2 = xarray.tests.test_plugins:backend_1", + ), + pkg_resources.EntryPoint.parse( + "dummy1 = xarray.tests.test_plugins:backend_1", + ), ] backend_entrypoints = plugins.build_engines(dummy_pkg_entrypoints) backend_entrypoints = list(backend_entrypoints) @@ -167,7 +171,8 @@ def test_no_matching_engine_found(): @mock.patch( - "xarray.backends.plugins.list_engines", mock.MagicMock(return_value={}), + "xarray.backends.plugins.list_engines", + mock.MagicMock(return_value={}), ) def test_no_engines_installed(): with pytest.raises(ValueError, match="no currently installed IO backends."): diff --git a/xarray/tests/test_testing.py b/xarray/tests/test_testing.py index 93aa9d63a8d..dc1db4dc8d7 100644 --- a/xarray/tests/test_testing.py +++ b/xarray/tests/test_testing.py @@ -95,7 +95,10 @@ def test_assert_duckarray_equal_failing(duckarray, obj1, obj2): @pytest.mark.parametrize( "duckarray", ( - pytest.param(np.array, id="numpy",), + pytest.param( + np.array, + id="numpy", + ), pytest.param( dask_from_array, id="dask", diff --git a/xarray/tests/test_units.py b/xarray/tests/test_units.py index 3091811f249..cc3c1a292ec 100644 --- a/xarray/tests/test_units.py +++ b/xarray/tests/test_units.py @@ -620,7 +620,9 @@ def test_align_dataset(value, unit, variant, error, dtype): units_a = extract_units(ds1) units_b = extract_units(ds2) expected_a, expected_b = func( - strip_units(ds1), strip_units(convert_units(ds2, units_a)), **stripped_kwargs, + strip_units(ds1), + strip_units(convert_units(ds2, units_a)), + **stripped_kwargs, ) expected_a = attach_units(expected_a, units_a) if isinstance(array2, Quantity): @@ -1731,7 +1733,10 @@ def test_missing_value_fillna(self, unit, error): pytest.param(1, id="no_unit"), pytest.param(unit_registry.dimensionless, id="dimensionless"), pytest.param(unit_registry.s, id="incompatible_unit"), - pytest.param(unit_registry.cm, id="compatible_unit",), + pytest.param( + unit_registry.cm, + id="compatible_unit", + ), pytest.param(unit_registry.m, id="identical_unit"), ), ) @@ -2177,7 +2182,8 @@ def test_pad(self, mode, xr_arg, np_arg): v = xr.Variable(["x", "y", "z"], data) expected = attach_units( - strip_units(v).pad(mode=mode, **xr_arg), extract_units(v), + strip_units(v).pad(mode=mode, **xr_arg), + extract_units(v), ) actual = v.pad(mode=mode, **xr_arg) @@ -2407,7 +2413,10 @@ def test_binary_operations(self, func, dtype): ( pytest.param(operator.lt, id="less_than"), pytest.param(operator.ge, id="greater_equal"), - pytest.param(operator.eq, id="equal",), + pytest.param( + operator.eq, + id="equal", + ), ), ) @pytest.mark.parametrize( @@ -2899,8 +2908,16 @@ def test_interpolate_na(self): unit_registry.dimensionless, DimensionalityError, id="dimensionless" ), pytest.param(unit_registry.s, DimensionalityError, id="incompatible_unit"), - pytest.param(unit_registry.cm, None, id="compatible_unit",), - pytest.param(unit_registry.m, None, id="identical_unit",), + pytest.param( + unit_registry.cm, + None, + id="compatible_unit", + ), + pytest.param( + unit_registry.m, + None, + id="identical_unit", + ), ), ) def test_combine_first(self, unit, error, dtype): @@ -3452,7 +3469,9 @@ def test_interp_reindex(self, variant, func, dtype): ), ) @pytest.mark.parametrize( - "func", (method("interp"), method("reindex")), ids=repr, + "func", + (method("interp"), method("reindex")), + ids=repr, ) def test_interp_reindex_indexing(self, func, unit, error, dtype): array = np.linspace(1, 2, 10).astype(dtype) @@ -3526,7 +3545,9 @@ def test_interp_reindex_like(self, variant, func, dtype): ), ) @pytest.mark.parametrize( - "func", (method("interp_like"), method("reindex_like")), ids=repr, + "func", + (method("interp_like"), method("reindex_like")), + ids=repr, ) def test_interp_reindex_like_indexing(self, func, unit, error, dtype): array = np.linspace(1, 2, 10).astype(dtype) @@ -3912,7 +3933,8 @@ def test_init(self, shared, unit, error, dtype): ( "data", pytest.param( - "dims", marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", + marks=pytest.mark.skip(reason="indexes don't support units"), ), "coords", ), @@ -4148,7 +4170,11 @@ def test_missing_value_filling(self, func, dtype): unit_registry.dimensionless, DimensionalityError, id="dimensionless" ), pytest.param(unit_registry.s, DimensionalityError, id="incompatible_unit"), - pytest.param(unit_registry.cm, None, id="compatible_unit",), + pytest.param( + unit_registry.cm, + None, + id="compatible_unit", + ), pytest.param(unit_registry.m, None, id="identical_unit"), ), ) @@ -4293,7 +4319,10 @@ def test_where(self, variant, unit, error, dtype): for key, value in kwargs.items() } - expected = attach_units(strip_units(ds).where(**kwargs_without_units), units,) + expected = attach_units( + strip_units(ds).where(**kwargs_without_units), + units, + ) actual = ds.where(**kwargs) assert_units_equal(expected, actual) @@ -4312,7 +4341,10 @@ def test_interpolate_na(self, dtype): ds = xr.Dataset({"a": ("x", array1), "b": ("x", array2)}) units = extract_units(ds) - expected = attach_units(strip_units(ds).interpolate_na(dim="x"), units,) + expected = attach_units( + strip_units(ds).interpolate_na(dim="x"), + units, + ) actual = ds.interpolate_na(dim="x") assert_units_equal(expected, actual) @@ -4335,7 +4367,8 @@ def test_interpolate_na(self, dtype): ( "data", pytest.param( - "dims", marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", + marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4354,7 +4387,8 @@ def test_combine_first(self, variant, unit, error, dtype): ) x = np.arange(len(array1)) * dims_unit ds = xr.Dataset( - data_vars={"a": ("x", array1), "b": ("x", array2)}, coords={"x": x}, + data_vars={"a": ("x", array1), "b": ("x", array2)}, + coords={"x": x}, ) units = extract_units(ds) @@ -4431,7 +4465,8 @@ def test_comparisons(self, func, variant, unit, dtype): y = coord * coord_unit ds = xr.Dataset( - data_vars={"a": ("x", a), "b": ("x", b)}, coords={"x": x, "y": ("x", y)}, + data_vars={"a": ("x", a), "b": ("x", b)}, + coords={"x": x, "y": ("x", y)}, ) units = extract_units(ds) @@ -4488,7 +4523,8 @@ def test_comparisons(self, func, variant, unit, dtype): ( "data", pytest.param( - "dims", marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", + marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4579,7 +4615,8 @@ def test_pad(self, dtype): ( "data", pytest.param( - "dims", marks=pytest.mark.skip(reason="indexes don't support units"), + "dims", + marks=pytest.mark.skip(reason="indexes don't support units"), ), ), ) @@ -4630,7 +4667,10 @@ def test_to_stacked_array(self, dtype): func = method("to_stacked_array", "z", variable_dim="y", sample_dims=["x"]) actual = func(ds).rename(None) - expected = attach_units(func(strip_units(ds)).rename(None), units,) + expected = attach_units( + func(strip_units(ds)).rename(None), + units, + ) assert_units_equal(expected, actual) assert_equal(expected, actual) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 1a439ab66ed..1e0dff45dd2 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -930,7 +930,8 @@ def test_rolling_1d(self): @pytest.mark.parametrize("dims", [("x", "y"), ("y", "z"), ("z", "x")]) def test_nd_rolling(self, center, dims): x = self.cls( - ("x", "y", "z"), np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), + ("x", "y", "z"), + np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), ) window = [3, 3] actual = x.rolling_window( @@ -961,11 +962,15 @@ def test_nd_rolling(self, center, dims): ) def test_rolling_window_errors(self, dim, window, window_dim, center): x = self.cls( - ("x", "y", "z"), np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), + ("x", "y", "z"), + np.arange(7 * 6 * 8).reshape(7, 6, 8).astype(float), ) with pytest.raises(ValueError): x.rolling_window( - dim=dim, window=window, window_dim=window_dim, center=center, + dim=dim, + window=window, + window_dim=window_dim, + center=center, ) @@ -2041,7 +2046,10 @@ def test_coarsen_keep_attrs(self, operation="mean"): # Test kept attrs with set_options(keep_attrs=True): new = Variable(["coord"], np.linspace(1, 10, 100), attrs=_attrs).coarsen( - windows={"coord": 1}, func=test_func, boundary="exact", side="left", + windows={"coord": 1}, + func=test_func, + boundary="exact", + side="left", ) assert new.attrs == _attrs @@ -2468,7 +2476,8 @@ def test_LazilyIndexedArray(self): self.check_vectorized_indexing(v) # doubly wrapping v = Variable( - dims=("x", "y"), data=LazilyIndexedArray(LazilyIndexedArray(self.d)), + dims=("x", "y"), + data=LazilyIndexedArray(LazilyIndexedArray(self.d)), ) self.check_orthogonal_indexing(v) # hierarchical wrapping diff --git a/xarray/tutorial.py b/xarray/tutorial.py index 03f61af7b65..78471be7a0e 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -73,7 +73,12 @@ def _check_netcdf_engine_installed(name): # idea borrowed from Seaborn def open_dataset( - name, cache=True, cache_dir=None, *, engine=None, **kws, + name, + cache=True, + cache_dir=None, + *, + engine=None, + **kws, ): """ Open a dataset from the online repository (requires internet). @@ -144,7 +149,11 @@ def open_dataset( def open_rasterio( - name, engine=None, cache=True, cache_dir=None, **kws, + name, + engine=None, + cache=True, + cache_dir=None, + **kws, ): """ Open a rasterio dataset from the online repository (requires internet).