# xarray.open_mfdataset

### xarray.open_mfdataset(paths, chunks=None, concat_dim=None, compat=no_conflicts, preprocess=None, engine=None, data_vars=all, coords=different, combine='by_coords', parallel=False, join=outer, attrs_file=None, combine_attrs='override', errors='raise', \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/backends/api.py#L1413-L1748)

Open multiple files as a single dataset.

If combine=’by_coords’ then the function `combine_by_coords` is used to combine
the datasets into one before returning the result, and if combine=’nested’ then
`combine_nested` is used. The filepaths must be structured according to which
combining function is used, the details of which are given in the documentation for
`combine_by_coords` and `combine_nested`. By default `combine='by_coords'`
will be used. Requires dask to be installed. See documentation for
details on dask <sup>[1](#id3)</sup>. Global attributes from the `attrs_file` are used
for the combined dataset.

* **Parameters:**
  * **paths** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or `nested sequence` of `paths`) – Either a string glob in the form `"path/to/my/files/*.nc"` or an explicit list of
    files to open. Paths can be given as strings or as pathlib Paths. If
    concatenation along more than one dimension is desired, then `paths` must be a
    nested list-of-lists (see `combine_nested` for details). (A string glob will
    be expanded to a 1-dimensional list.)
  * **chunks** ([`int`](https://docs.python.org/3/builtins/functions.html#int), [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), `'auto'` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dictionary with keys given by dimension names and values given by chunk sizes.
    In general, these should divide the dimensions of each dataset. If int, chunk
    each dimension by `chunks`. By default, chunks will be chosen to match the
    chunks on disk. This may impact performance: please see the full documentation
    for more details <sup>[2](#id4)</sup>. This argument is evaluated on a per-file basis, so chunk
    sizes that span multiple files will be ignored.
  * **concat_dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [`Index`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.html#pandas.Index) or `a Sequence` of `these` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dimensions to concatenate files along.  You only need to provide this argument
    if `combine='nested'`, and if any of the dimensions along which you want to
    concatenate is not a dimension in the original datasets, e.g., if you want to
    stack a collection of 2D arrays along a third dimension. Set
    `concat_dim=[..., None, ...]` explicitly to disable concatenation along a
    particular dimension. Default is None, which for a 1D list of filepaths is
    equivalent to opening the files separately and then merging them with
    `xarray.merge`.
  * **combine** (`{"by_coords", "nested"}`, *optional*) – Whether `xarray.combine_by_coords` or `xarray.combine_nested` is used to
    combine all the data. Default is to use `xarray.combine_by_coords`.
  * **compat** (`{"identical", "equals", "broadcast_equals", "no_conflicts", "override"}`, *default*: `"no_conflicts"`) – String indicating how to compare variables of the same name for
    potential conflicts when merging:
    > * “broadcast_equals”: all values must be equal when variables are
    >   broadcast against each other to ensure common dimensions.
    > * “equals”: all values and dimensions must be the same.
    > * “identical”: all values, dimensions and attributes must be the
    >   same.
    > * “no_conflicts”: only values which are not null in both datasets
    >   must be equal. The returned dataset then contains the combination
    >   of all non-null values.
    > * “override”: skip comparing and pick variable from first dataset
  * **preprocess** ([`callable()`](https://docs.python.org/3/builtins/functions.html#callable), *optional*) – If provided, call this function on each dataset prior to concatenation.
    You can find the file-name from which each dataset was loaded in
    `ds.encoding["source"]`.
  * **engine** (`{"netcdf4", "scipy", "pydap", "h5netcdf", "zarr", None}`        , installed backend         or `subclass` of [`xarray.backends.BackendEntrypoint`](xarray.backends.BackendEntrypoint.html.md#xarray.backends.BackendEntrypoint), *optional*) – Engine to use when reading files. If not provided, the default engine
    is chosen based on available dependencies, by default preferring
    “netcdf4” over “h5netcdf” over “scipy” (customizable via
    `netcdf_engine_order` in `xarray.set_options()`). A custom backend
    class (a subclass of `BackendEntrypoint`) can also be used.
  * **data_vars** (`{"minimal", "different", "all", None}` or [`list`](https://docs.python.org/3/builtins/stdtypes.html#list) of [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), *default*: `"all"`) –

    These data variables will be concatenated together:
    : * “minimal”: Only data variables in which the dimension already
        appears are included.
      * “different”: Data variables which are not equal (ignoring
        attributes) across all datasets are also concatenated (as well as
        all for which dimension already appears). Beware: this option may
        load the data payload of data variables into memory if they are not
        already loaded.
      * “all”: All data variables will be concatenated.
      * None: Means `"all"` if `concat_dim` is not present in any of
        the `objs`, and `"minimal"` if `concat_dim` is present
        in any of `objs`.
      * list of str: The listed data variables will be concatenated, in
        addition to the “minimal” data variables.
  * **coords** (`{"minimal", "different", "all"}` or [`list`](https://docs.python.org/3/builtins/stdtypes.html#list) of [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), *default*: `"different"`) –

    These coordinate variables will be concatenated together:
    : * “minimal”: Only coordinates in which the dimension already appears
        are included.
      * “different”: Coordinates which are not equal (ignoring attributes)
        across all datasets are also concatenated (as well as all for which
        dimension already appears). Beware: this option may load the data
        payload of coordinate variables into memory if they are not already
        loaded.
      * “all”: All coordinate variables will be concatenated, except
        those corresponding to other dimensions.
      * list of str: The listed coordinate variables will be concatenated,
        in addition the “minimal” coordinates.
  * **parallel** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, the open and preprocess steps of this function will be
    performed in parallel using `dask.delayed`. Default is False.
  * **join** (`{"outer", "inner", "left", "right", "exact", "override"}`, *default*: `"outer"`) – String indicating how to combine differing indexes
    (excluding concat_dim) in objects
    - “outer”: use the union of object indexes
    - “inner”: use the intersection of object indexes
    - “left”: use indexes from the first object with each dimension
    - “right”: use indexes from the last object with each dimension
    - “exact”: instead of aligning, raise ValueError when indexes to be
      aligned are not equal
    - “override”: if indexes are of same size, rewrite indexes to be
      those of the first object with that dimension. Indexes for the same
      dimension must have the same size in all objects.
  * **attrs_file** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or [path-like](https://docs.python.org/3/glossary.html#term-path-like-object), *optional*) – Path of the file used to read global attributes from.
    By default global attributes are read from the first file provided,
    with wildcard matches sorted by filename.
  * **combine_attrs** (`{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}` or [`callable()`](https://docs.python.org/3/builtins/functions.html#callable), *default*: `"override"`) – A callable or a string indicating how to combine attrs of the objects being
    merged:
    - “drop”: empty attrs on returned Dataset.
    - “identical”: all attrs must be the same on every object.
    - “no_conflicts”: attrs from all objects are combined, any that have
      the same name must also have the same value.
    - “drop_conflicts”: attrs from all objects are combined, any that have
      the same name but different values are dropped.
    - “override”: skip comparing and copy attrs from the first dataset to
      the result.

    If a callable, it must expect a sequence of `attrs` dicts and a context object
    as its only parameters.
  * **errors** (`{"raise", "warn", "ignore"}`, *default*: `"raise"`) – String indicating how to handle errors in opening dataset.
    - “raise”: invalid dataset will raise an exception.
    - “warn”: a warning will be issued for each invalid dataset.
    - “ignore”: invalid dataset will be ignored.
  * **\*\*kwargs** (**optional**) – Additional arguments passed on to [`xarray.open_dataset()`](xarray.open_dataset.html.md#xarray.open_dataset). For an
    overview of some of the possible options, see the documentation of
    [`xarray.open_dataset()`](xarray.open_dataset.html.md#xarray.open_dataset)
* **Returns:**
  [`xarray.Dataset`](xarray.Dataset.html.md#xarray.Dataset)

### Notes

`open_mfdataset` opens files with read-only access. When you modify values
of a Dataset, even one linked to files on disk, only the in-memory copy you
are manipulating in xarray is modified: the original file on disk is never
touched.

#### SEE ALSO
[`combine_by_coords`](xarray.combine_by_coords.html.md#xarray.combine_by_coords), [`combine_nested`](xarray.combine_nested.html.md#xarray.combine_nested), [`open_dataset`](xarray.open_dataset.html.md#xarray.open_dataset)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('53c69476-6e4b-4933-a133-36297bcefebb','f9b1aa63-c39b-4976-bbf6-f964b693088f','a1dd7963-7eb2-4b2e-809e-fb72c0afcee7','../lite/tree/../notebooks/index.html?path=d20a0a5b_f3b3_47f5_943a_c2e1f6e7d315.ipynb','None')">Try it in your browser!</button></div>

A user might want to pass additional arguments into `preprocess` when
applying some operation to many individual files that are being opened. One route
to do this is through the use of `functools.partial`.

```pycon
>>> from functools import partial
>>> def _preprocess(x, lon_bnds, lat_bnds):
...     return x.sel(lon=slice(*lon_bnds), lat=slice(*lat_bnds))
...
>>> lon_bnds, lat_bnds = (-110, -105), (40, 45)
>>> partial_func = partial(_preprocess, lon_bnds=lon_bnds, lat_bnds=lat_bnds)
>>> ds = xr.open_mfdataset(
...     "file_*.nc", concat_dim="time", preprocess=partial_func
... )
```

It is also possible to use any argument to `open_dataset` together
with `open_mfdataset`, such as for example `drop_variables`:

```pycon
>>> ds = xr.open_mfdataset(
...     "file.nc", drop_variables=["varname_1", "varname_2"]  # any list of vars
... )
```

<div id="a1dd7963-7eb2-4b2e-809e-fb72c0afcee7" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('53c69476-6e4b-4933-a133-36297bcefebb','a1dd7963-7eb2-4b2e-809e-fb72c0afcee7')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('53c69476-6e4b-4933-a133-36297bcefebb','a1dd7963-7eb2-4b2e-809e-fb72c0afcee7')">Open In Tab</button></div><div id="f9b1aa63-c39b-4976-bbf6-f964b693088f" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

### References

* <a id='id3'>**[1]**</a> [https://docs.xarray.dev/en/stable/dask.html](https://docs.xarray.dev/en/stable/dask.html)
* <a id='id4'>**[2]**</a> [https://docs.xarray.dev/en/stable/dask.html#chunking-and-performance](https://docs.xarray.dev/en/stable/dask.html#chunking-and-performance)
