Skip to content

Commit e5d163a

Browse files
authored
Rename to_array to to_dataarray (#8438)
1 parent 0ba2eb0 commit e5d163a

15 files changed

+58
-40
lines changed

doc/api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ Dataset methods
603603
Dataset.as_numpy
604604
Dataset.from_dataframe
605605
Dataset.from_dict
606-
Dataset.to_array
606+
Dataset.to_dataarray
607607
Dataset.to_dataframe
608608
Dataset.to_dask_dataframe
609609
Dataset.to_dict

doc/howdoi.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ How do I ...
3636
* - rename a variable, dimension or coordinate
3737
- :py:meth:`Dataset.rename`, :py:meth:`DataArray.rename`, :py:meth:`Dataset.rename_vars`, :py:meth:`Dataset.rename_dims`,
3838
* - convert a DataArray to Dataset or vice versa
39-
- :py:meth:`DataArray.to_dataset`, :py:meth:`Dataset.to_array`, :py:meth:`Dataset.to_stacked_array`, :py:meth:`DataArray.to_unstacked_dataset`
39+
- :py:meth:`DataArray.to_dataset`, :py:meth:`Dataset.to_dataarray`, :py:meth:`Dataset.to_stacked_array`, :py:meth:`DataArray.to_unstacked_dataset`
4040
* - extract variables that have certain attributes
4141
- :py:meth:`Dataset.filter_by_attrs`
4242
* - extract the underlying array (e.g. NumPy or Dask arrays)

doc/user-guide/reshaping.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ use :py:meth:`~xarray.DataArray.squeeze`
5959
Converting between datasets and arrays
6060
--------------------------------------
6161

62-
To convert from a Dataset to a DataArray, use :py:meth:`~xarray.Dataset.to_array`:
62+
To convert from a Dataset to a DataArray, use :py:meth:`~xarray.Dataset.to_dataarray`:
6363

6464
.. ipython:: python
6565
66-
arr = ds.to_array()
66+
arr = ds.to_dataarray()
6767
arr
6868
6969
This method broadcasts all data variables in the dataset against each other,
@@ -77,7 +77,7 @@ To convert back from a DataArray to a Dataset, use
7777
7878
arr.to_dataset(dim="variable")
7979
80-
The broadcasting behavior of ``to_array`` means that the resulting array
80+
The broadcasting behavior of ``to_dataarray`` means that the resulting array
8181
includes the union of data variable dimensions:
8282

8383
.. ipython:: python
@@ -88,7 +88,7 @@ includes the union of data variable dimensions:
8888
ds2
8989
9090
# the resulting array has 6 elements
91-
ds2.to_array()
91+
ds2.to_dataarray()
9292
9393
Otherwise, the result could not be represented as an orthogonal array.
9494

@@ -161,8 +161,8 @@ arrays as inputs. For datasets with only one variable, we only need ``stack``
161161
and ``unstack``, but combining multiple variables in a
162162
:py:class:`xarray.Dataset` is more complicated. If the variables in the dataset
163163
have matching numbers of dimensions, we can call
164-
:py:meth:`~xarray.Dataset.to_array` and then stack along the the new coordinate.
165-
But :py:meth:`~xarray.Dataset.to_array` will broadcast the dataarrays together,
164+
:py:meth:`~xarray.Dataset.to_dataarray` and then stack along the the new coordinate.
165+
But :py:meth:`~xarray.Dataset.to_dataarray` will broadcast the dataarrays together,
166166
which will effectively tile the lower dimensional variable along the missing
167167
dimensions. The method :py:meth:`xarray.Dataset.to_stacked_array` allows
168168
combining variables of differing dimensions without this wasteful copying while

doc/whats-new.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ Deprecations
4141
this was one place in the API where dimension positions were used.
4242
(:pull:`8341`)
4343
By `Maximilian Roos <https://github.com/max-sixty>`_.
44+
- Rename :py:meth:`Dataset.to_array` to :py:meth:`Dataset.to_dataarray` for
45+
consistency with :py:meth:`DataArray.to_dataset` &
46+
:py:func:`open_dataarray` functions. This is a "soft" deprecation — the
47+
existing methods work and don't raise any warnings, given the relatively small
48+
benefits of the change.
49+
By `Maximilian Roos <https://github.com/max-sixty>`_.
4450

4551
Bug fixes
4652
~~~~~~~~~
@@ -6709,7 +6715,7 @@ Backwards incompatible changes
67096715
Enhancements
67106716
~~~~~~~~~~~~
67116717

6712-
- New ``xray.Dataset.to_array`` and enhanced
6718+
- New ``xray.Dataset.to_dataarray`` and enhanced
67136719
``xray.DataArray.to_dataset`` methods make it easy to switch back
67146720
and forth between arrays and datasets:
67156721

@@ -6720,8 +6726,8 @@ Enhancements
67206726
coords={"c": 42},
67216727
attrs={"Conventions": "None"},
67226728
)
6723-
ds.to_array()
6724-
ds.to_array().to_dataset(dim="variable")
6729+
ds.to_dataarray()
6730+
ds.to_dataarray().to_dataset(dim="variable")
67256731
67266732
- New ``xray.Dataset.fillna`` method to fill missing values, modeled
67276733
off the pandas method of the same name:

xarray/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ def _dataset_indexer(dim: Hashable) -> DataArray:
11731173
var for var in cond if dim not in cond[var].dims
11741174
)
11751175
keepany = cond_wdim.any(dim=(d for d in cond.dims.keys() if d != dim))
1176-
return keepany.to_array().any("variable")
1176+
return keepany.to_dataarray().any("variable")
11771177

11781178
_get_indexer = (
11791179
_dataarray_indexer if isinstance(cond, DataArray) else _dataset_indexer

xarray/core/computation.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,9 @@ def cross(
16031603
>>> ds_a = xr.Dataset(dict(x=("dim_0", [1]), y=("dim_0", [2]), z=("dim_0", [3])))
16041604
>>> ds_b = xr.Dataset(dict(x=("dim_0", [4]), y=("dim_0", [5]), z=("dim_0", [6])))
16051605
>>> c = xr.cross(
1606-
... ds_a.to_array("cartesian"), ds_b.to_array("cartesian"), dim="cartesian"
1606+
... ds_a.to_dataarray("cartesian"),
1607+
... ds_b.to_dataarray("cartesian"),
1608+
... dim="cartesian",
16071609
... )
16081610
>>> c.to_dataset(dim="cartesian")
16091611
<xarray.Dataset>

xarray/core/dataset.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ def __array__(self, dtype=None):
15021502
"cannot directly convert an xarray.Dataset into a "
15031503
"numpy array. Instead, create an xarray.DataArray "
15041504
"first, either with indexing on the Dataset or by "
1505-
"invoking the `to_array()` method."
1505+
"invoking the `to_dataarray()` method."
15061506
)
15071507

15081508
@property
@@ -5260,7 +5260,7 @@ def to_stacked_array(
52605260
"""Combine variables of differing dimensionality into a DataArray
52615261
without broadcasting.
52625262
5263-
This method is similar to Dataset.to_array but does not broadcast the
5263+
This method is similar to Dataset.to_dataarray but does not broadcast the
52645264
variables.
52655265
52665266
Parameters
@@ -5289,7 +5289,7 @@ def to_stacked_array(
52895289
52905290
See Also
52915291
--------
5292-
Dataset.to_array
5292+
Dataset.to_dataarray
52935293
Dataset.stack
52945294
DataArray.to_unstacked_dataset
52955295
@@ -7019,7 +7019,7 @@ def assign(
70197019

70207020
return data
70217021

7022-
def to_array(
7022+
def to_dataarray(
70237023
self, dim: Hashable = "variable", name: Hashable | None = None
70247024
) -> DataArray:
70257025
"""Convert this dataset into an xarray.DataArray
@@ -7056,6 +7056,12 @@ def to_array(
70567056

70577057
return DataArray._construct_direct(variable, coords, name, indexes)
70587058

7059+
def to_array(
7060+
self, dim: Hashable = "variable", name: Hashable | None = None
7061+
) -> DataArray:
7062+
"""Deprecated version of to_dataarray"""
7063+
return self.to_dataarray(dim=dim, name=name)
7064+
70597065
def _normalize_dim_order(
70607066
self, dim_order: Sequence[Hashable] | None = None
70617067
) -> dict[Hashable, int]:

xarray/core/groupby.py

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ def to_dataarray(self) -> DataArray:
251251
data=self.data, dims=(self.name,), coords=self.coords, name=self.name
252252
)
253253

254+
def to_array(self) -> DataArray:
255+
"""Deprecated version of to_dataarray."""
256+
return self.to_dataarray()
257+
254258

255259
T_Group = Union["T_DataArray", "IndexVariable", _DummyGroup]
256260

xarray/tests/test_concat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1070,10 +1070,10 @@ def test_concat_fill_value(self, fill_value) -> None:
10701070
def test_concat_join_kwarg(self) -> None:
10711071
ds1 = Dataset(
10721072
{"a": (("x", "y"), [[0]])}, coords={"x": [0], "y": [0]}
1073-
).to_array()
1073+
).to_dataarray()
10741074
ds2 = Dataset(
10751075
{"a": (("x", "y"), [[0]])}, coords={"x": [1], "y": [0.0001]}
1076-
).to_array()
1076+
).to_dataarray()
10771077

10781078
expected: dict[JoinOptions, Any] = {}
10791079
expected["outer"] = Dataset(
@@ -1101,7 +1101,7 @@ def test_concat_join_kwarg(self) -> None:
11011101

11021102
for join in expected:
11031103
actual = concat([ds1, ds2], join=join, dim="x")
1104-
assert_equal(actual, expected[join].to_array())
1104+
assert_equal(actual, expected[join].to_dataarray())
11051105

11061106
def test_concat_combine_attrs_kwarg(self) -> None:
11071107
da1 = DataArray([0], coords=[("x", [0])], attrs={"b": 42})
@@ -1224,7 +1224,7 @@ def test_concat_preserve_coordinate_order() -> None:
12241224

12251225
def test_concat_typing_check() -> None:
12261226
ds = Dataset({"foo": 1}, {"bar": 2})
1227-
da = Dataset({"foo": 3}, {"bar": 4}).to_array(dim="foo")
1227+
da = Dataset({"foo": 3}, {"bar": 4}).to_dataarray(dim="foo")
12281228

12291229
# concatenate a list of non-homogeneous types must raise TypeError
12301230
with pytest.raises(

xarray/tests/test_dask.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,11 @@ def test_to_dataset_roundtrip(self):
608608
v = self.lazy_array
609609

610610
expected = u.assign_coords(x=u["x"])
611-
self.assertLazyAndEqual(expected, v.to_dataset("x").to_array("x"))
611+
self.assertLazyAndEqual(expected, v.to_dataset("x").to_dataarray("x"))
612612

613613
def test_merge(self):
614614
def duplicate_and_merge(array):
615-
return xr.merge([array, array.rename("bar")]).to_array()
615+
return xr.merge([array, array.rename("bar")]).to_dataarray()
616616

617617
expected = duplicate_and_merge(self.eager_array)
618618
actual = duplicate_and_merge(self.lazy_array)
@@ -1306,12 +1306,12 @@ def test_map_blocks_kwargs(obj):
13061306
assert_identical(actual, expected)
13071307

13081308

1309-
def test_map_blocks_to_array(map_ds):
1309+
def test_map_blocks_to_dataarray(map_ds):
13101310
with raise_if_dask_computes():
1311-
actual = xr.map_blocks(lambda x: x.to_array(), map_ds)
1311+
actual = xr.map_blocks(lambda x: x.to_dataarray(), map_ds)
13121312

1313-
# to_array does not preserve name, so cannot use assert_identical
1314-
assert_equal(actual, map_ds.to_array())
1313+
# to_dataarray does not preserve name, so cannot use assert_identical
1314+
assert_equal(actual, map_ds.to_dataarray())
13151315

13161316

13171317
@pytest.mark.parametrize(
@@ -1376,8 +1376,8 @@ def test_map_blocks_template_convert_object():
13761376
assert_identical(actual, template)
13771377

13781378
ds = da.to_dataset()
1379-
func = lambda x: x.to_array().isel(x=[1])
1380-
template = ds.to_array().isel(x=[1, 5, 9])
1379+
func = lambda x: x.to_dataarray().isel(x=[1])
1380+
template = ds.to_dataarray().isel(x=[1, 5, 9])
13811381
with raise_if_dask_computes():
13821382
actual = xr.map_blocks(func, ds, template=template)
13831383
assert_identical(actual, template)

xarray/tests/test_dataarray.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3801,7 +3801,7 @@ def test_to_dataset_split(self) -> None:
38013801
with pytest.raises(TypeError):
38023802
array.to_dataset("x", name="foo")
38033803

3804-
roundtripped = actual.to_array(dim="x")
3804+
roundtripped = actual.to_dataarray(dim="x")
38053805
assert_identical(array, roundtripped)
38063806

38073807
array = DataArray([1, 2, 3], dims="x")
@@ -3818,7 +3818,7 @@ def test_to_dataset_retains_keys(self) -> None:
38183818
array = DataArray([1, 2, 3], coords=[("x", dates)], attrs={"a": 1})
38193819

38203820
# convert to dateset and back again
3821-
result = array.to_dataset("x").to_array(dim="x")
3821+
result = array.to_dataset("x").to_dataarray(dim="x")
38223822

38233823
assert_equal(array, result)
38243824

xarray/tests/test_dataset.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4569,7 +4569,7 @@ def test_squeeze_drop(self) -> None:
45694569
selected = data.squeeze(drop=True)
45704570
assert_identical(data, selected)
45714571

4572-
def test_to_array(self) -> None:
4572+
def test_to_dataarray(self) -> None:
45734573
ds = Dataset(
45744574
{"a": 1, "b": ("x", [1, 2, 3])},
45754575
coords={"c": 42},
@@ -4579,10 +4579,10 @@ def test_to_array(self) -> None:
45794579
coords = {"c": 42, "variable": ["a", "b"]}
45804580
dims = ("variable", "x")
45814581
expected = DataArray(data, coords, dims, attrs=ds.attrs)
4582-
actual = ds.to_array()
4582+
actual = ds.to_dataarray()
45834583
assert_identical(expected, actual)
45844584

4585-
actual = ds.to_array("abc", name="foo")
4585+
actual = ds.to_dataarray("abc", name="foo")
45864586
expected = expected.rename({"variable": "abc"}).rename("foo")
45874587
assert_identical(expected, actual)
45884588

xarray/tests/test_groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -600,19 +600,19 @@ def test_groupby_grouping_errors() -> None:
600600
with pytest.raises(
601601
ValueError, match=r"None of the data falls within bins with edges"
602602
):
603-
dataset.to_array().groupby_bins("x", bins=[0.1, 0.2, 0.3])
603+
dataset.to_dataarray().groupby_bins("x", bins=[0.1, 0.2, 0.3])
604604

605605
with pytest.raises(ValueError, match=r"All bin edges are NaN."):
606606
dataset.groupby_bins("x", bins=[np.nan, np.nan, np.nan])
607607

608608
with pytest.raises(ValueError, match=r"All bin edges are NaN."):
609-
dataset.to_array().groupby_bins("x", bins=[np.nan, np.nan, np.nan])
609+
dataset.to_dataarray().groupby_bins("x", bins=[np.nan, np.nan, np.nan])
610610

611611
with pytest.raises(ValueError, match=r"Failed to group data."):
612612
dataset.groupby(dataset.foo * np.nan)
613613

614614
with pytest.raises(ValueError, match=r"Failed to group data."):
615-
dataset.to_array().groupby(dataset.foo * np.nan)
615+
dataset.to_dataarray().groupby(dataset.foo * np.nan)
616616

617617

618618
def test_groupby_reduce_dimension_error(array) -> None:

xarray/tests/test_rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def test_rolling_construct(self, center: bool, window: int) -> None:
631631
ds_rolling_mean = ds_rolling.construct("window", stride=2, fill_value=0.0).mean(
632632
"window"
633633
)
634-
assert (ds_rolling_mean.isnull().sum() == 0).to_array(dim="vars").all()
634+
assert (ds_rolling_mean.isnull().sum() == 0).to_dataarray(dim="vars").all()
635635
assert (ds_rolling_mean["x"] == 0.0).sum() >= 0
636636

637637
@pytest.mark.parametrize("center", (True, False))

xarray/tests/test_sparse.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def setUp(self):
578578

579579
def test_to_dataset_roundtrip(self):
580580
x = self.sp_xr
581-
assert_equal(x, x.to_dataset("x").to_array("x"))
581+
assert_equal(x, x.to_dataset("x").to_dataarray("x"))
582582

583583
def test_align(self):
584584
a1 = xr.DataArray(
@@ -830,7 +830,7 @@ def test_reindex(self):
830830
@pytest.mark.xfail
831831
def test_merge(self):
832832
x = self.sp_xr
833-
y = xr.merge([x, x.rename("bar")]).to_array()
833+
y = xr.merge([x, x.rename("bar")]).to_dataarray()
834834
assert isinstance(y, sparse.SparseArray)
835835

836836
@pytest.mark.xfail

0 commit comments

Comments
 (0)