Skip to content

Dataset.map no longer handles non-DataArray outputs #10835

@shoyer

Description

@shoyer

What happened?

In old versions of Xarray, Dataset.map handled non-DataArray outputs by casting them into DataArrays, using the xarray.Dataset constructor.

This is no longer the case after #10602. Now a mysterious error is raised instead.

What did you expect to happen?

I'm not sure this is exactly a bug, because this behavior is not documented (and apparently not tested, either!). Nonetheless, some users were relying on it.

We should either continue to support non-DataArray outputs, or raise an informative error if the return value of func is not a DataArray.

Minimal Complete Verifiable Example

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.

import xarray as xr

xr.show_versions()

ds = xr.Dataset({'foo': ('x', [1, 2, 3])})
ds.map(lambda x: 1)

Steps to reproduce

Here's the error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[/tmp/ipython-input-563895862.py](https://localhost:8080/#) in <cell line: 0>()
      2 
      3 ds = xarray.Dataset({'foo': ('x', [1, 2, 3])})
----> 4 ds.map(lambda x: 1)

[/usr/local/lib/python3.12/dist-packages/xarray/core/dataset.py](https://localhost:8080/#) in map(self, func, keep_attrs, args, **kwargs)
   6937         }
   6938         coord_vars, indexes = merge_coordinates_without_align(
-> 6939             [v.coords for v in variables.values()]
   6940         )
   6941         coords = Coordinates._construct_direct(coords=coord_vars, indexes=indexes)

AttributeError: 'int' object has no attribute 'coords'

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Anything else we need to know?

No response

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions