Skip to content

Commit 1cad803

Browse files
hsharrisonshoyer
authored andcommitted
add assign_attrs method (#1286)
* add assign_attrs method * Add assign_attrs doc links. * Fix docstring formatting.
1 parent afbdbc7 commit 1cad803

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Dataset contents
7575
Dataset.copy
7676
Dataset.assign
7777
Dataset.assign_coords
78+
Dataset.assign_attrs
7879
Dataset.pipe
7980
Dataset.merge
8081
Dataset.rename
@@ -217,6 +218,7 @@ DataArray contents
217218
:toctree: generated/
218219

219220
DataArray.assign_coords
221+
DataArray.assign_attrs
220222
DataArray.pipe
221223
DataArray.rename
222224
DataArray.swap_dims

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Enhancements
3030
specified with `x=` or `y=`, the other dimension is now guessed. By
3131
`Vincent Noel <https://github.com/vnoel>`_.
3232

33+
Added new method :py:meth:`~Dataset.assign_attrs` to ``DataArray`` and
34+
``Dataset``, a chained-method compatible implementation of the
35+
``dict.update`` method on attrs (:issue:`1281`).
36+
By `Henry S. Harrison <https://hsharrison.github.io>`_.
37+
3338
Bug fixes
3439
~~~~~~~~~
3540
- ``rolling`` now keeps its original dimension order (:issue:`1125`).

xarray/core/common.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,10 @@ def _calc_assign_results(self, kwargs):
312312
return results
313313

314314
def assign_coords(self, **kwargs):
315-
"""Assign new coordinates to this object, returning a new object
316-
with all the original data in addition to the new coordinates.
315+
"""Assign new coordinates to this object.
316+
317+
Returns a new object with all the original data in addition to the new
318+
coordinates.
317319
318320
Parameters
319321
----------
@@ -346,6 +348,29 @@ def assign_coords(self, **kwargs):
346348
data.coords.update(results)
347349
return data
348350

351+
def assign_attrs(self, *args, **kwargs):
352+
"""Assign new attrs to this object.
353+
354+
Returns a new object equivalent to self.attrs.update(*args, **kwargs).
355+
356+
Parameters
357+
----------
358+
args : positional arguments passed into ``attrs.update``.
359+
kwargs : keyword arguments passed into ``attrs.update``.
360+
361+
Returns
362+
-------
363+
assigned : same type as caller
364+
A new object with the new attrs in addition to the existing data.
365+
366+
See also
367+
--------
368+
Dataset.assign
369+
"""
370+
out = self.copy(deep=False)
371+
out.attrs.update(*args, **kwargs)
372+
return out
373+
349374
def pipe(self, func, *args, **kwargs):
350375
"""
351376
Apply func(self, *args, **kwargs)

xarray/tests/test_dataarray.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,20 @@ def test_reduce_keep_attrs(self):
13521352
self.assertEqual(len(vm.attrs), len(self.attrs))
13531353
self.assertEqual(vm.attrs, self.attrs)
13541354

1355+
def test_assign_attrs(self):
1356+
expected = DataArray([], attrs=dict(a=1, b=2))
1357+
expected.attrs['a'] = 1
1358+
expected.attrs['b'] = 2
1359+
new = DataArray([])
1360+
actual = DataArray([]).assign_attrs(a=1, b=2)
1361+
self.assertDatasetIdentical(actual, expected)
1362+
self.assertEqual(new.attrs, {})
1363+
1364+
expected.attrs['c'] = 3
1365+
new_actual = actual.assign_attrs({'c': 3})
1366+
self.assertDatasetIdentical(new_actual, expected)
1367+
self.assertEqual(actual.attrs, {'a': 1, 'b': 2})
1368+
13551369
def test_fillna(self):
13561370
a = DataArray([np.nan, 1, np.nan, 3], coords={'x': range(4)}, dims='x')
13571371
actual = a.fillna(-1)

xarray/tests/test_dataset.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,18 @@ def test_assign(self):
18481848
expected = expected.set_coords('c')
18491849
self.assertDatasetIdentical(actual, expected)
18501850

1851+
def test_assign_attrs(self):
1852+
expected = Dataset(attrs=dict(a=1, b=2))
1853+
new = Dataset()
1854+
actual = new.assign_attrs(a=1, b=2)
1855+
self.assertDatasetIdentical(actual, expected)
1856+
self.assertEqual(new.attrs, {})
1857+
1858+
expected.attrs['c'] = 3
1859+
new_actual = actual.assign_attrs({'c': 3})
1860+
self.assertDatasetIdentical(new_actual, expected)
1861+
self.assertEqual(actual.attrs, dict(a=1, b=2))
1862+
18511863
def test_assign_multiindex_level(self):
18521864
data = create_test_multiindex()
18531865
with self.assertRaisesRegexp(ValueError, 'conflicting MultiIndex'):

0 commit comments

Comments
 (0)