Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Enhancements
Bug fixes
~~~~~~~~~

- Dataset.copy(deep=True) now creates a deep copy of the attrs (:issue:`2835`).
By `Andras Gefferth <https://github.com/kefirbandi>`_.
- ``swap_dims`` would create incorrect ``indexes`` (:issue:`2842`).
By `Stephan Hoyer <https://github.com/shoyer>`_.


.. _whats-new.0.12.0:

v0.12.0 (15 March 2019)
Expand Down
4 changes: 3 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,9 @@ def copy(self: T, deep: bool = False, data: Mapping = None) -> T:
variables = OrderedDict((k, v.copy(deep=deep, data=data.get(k)))
for k, v in self._variables.items())

return self._replace(variables)
attrs = copy.deepcopy(self._attrs) if deep else copy.copy(self._attrs)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made one small fix, renaming _attrs to attrs.

As a general rule, we use the underscore prefix in Python as a convention for "private" but technically public variables. This isn't necessary for local variables defined within the scope of a function, since these are already private.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Actually I first called it "new_attrs" but it was 3 characters above PEP8 line-length limit, so I removed the first 3... :)
Thanks for the fix.


return self._replace(variables, attrs=attrs)

@property
def _level_coords(self):
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ def test_drop_dims(self):

def test_copy(self):
data = create_test_data()
data.attrs['Test'] = [1, 2, 3]

for copied in [data.copy(deep=False), copy(data)]:
assert_identical(data, copied)
Expand All @@ -1899,12 +1900,18 @@ def test_copy(self):
copied['foo'] = ('z', np.arange(5))
assert 'foo' not in data

copied.attrs['foo'] = 'bar'
assert 'foo' not in data.attrs
assert data.attrs['Test'] is copied.attrs['Test']

for copied in [data.copy(deep=True), deepcopy(data)]:
assert_identical(data, copied)
for k, v0 in data.variables.items():
v1 = copied.variables[k]
assert v0 is not v1

assert data.attrs['Test'] is not copied.attrs['Test']
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a test for shallow copying?


def test_copy_with_data(self):
orig = create_test_data()
new_data = {k: np.random.randn(*v.shape)
Expand Down