Skip to content

.where() and .fillna() should preserve attributes #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 21, 2016

Conversation

fmaussion
Copy link
Member

Fixes #1009

There is probably a more elegant way to do it, but the internals of xarray still confuse me. I'm happy to learn!

# preserve attributes
out = outobj._where(outcond)
out.attrs = self.attrs
if isinstance(out, Dataset):
Copy link
Member

Choose a reason for hiding this comment

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

Can you implement a private method defined on both Dataset/DataArray, something like _copy_attrs_from(self, other)? Then we can call out._copy_attrs_from(self) for a generic solution.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, good idea. Should the Dataset version check for the compatibility of other, or should we just assume that other will always have the same variables?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine to assume compatibility, given that this is only being called by internal methods. If other is not a Dataset then something has gone seriously wrong.

On the other hand, I think it might work to call DataArray.where with a Dataset as the argument -- worth double checking.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just tried actual = ds.a.where(ds > 1)and _where() returns a NotImplemented string

Copy link
Member

@shoyer shoyer Sep 20, 2016

Choose a reason for hiding this comment

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

OK, I think we're safe then, though we probably shouldn't be returning NotImplemented singletons to users :).

out.attrs = self.attrs
if isinstance(out, Dataset):
for v in out:
out[v].attrs = self[v].attrs
Copy link
Member

Choose a reason for hiding this comment

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

Use out.variables[v].attrs = self.variables[v].attrs instead. Creating DataArray objects is pretty expensive compared to just pulling out existing Variable objects.

@@ -1001,7 +1001,9 @@ def fillna(self, value):
if utils.is_dict_like(value):
raise TypeError('cannot provide fill value as a dictionary with '
'fillna on a DataArray')
return self._fillna(value)
out = self._fillna(value)
Copy link
Member

Choose a reason for hiding this comment

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

probably can put this in _fillna instead.

ds.attrs['attr'] = 'ds'
ds.a.attrs['attr'] = 'da'
actual = ds.groupby('b').fillna(Dataset({'a': ('b', [0, 2])}))
self.assertTrue('attr' in actual.attrs)
Copy link
Member

@shoyer shoyer Sep 20, 2016

Choose a reason for hiding this comment

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

use something like self.assertEquals(actual.attrs, {'attr': 'ds'}) instead

actual = ds.groupby('b').fillna(Dataset({'a': ('b', [0, 2])}))
self.assertTrue('attr' in actual.attrs)
self.assertTrue(actual.attrs['attr'] == 'ds')
self.assertTrue(actual.a.name == 'a')
Copy link
Member

Choose a reason for hiding this comment

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

use self.assertEquals again

@fmaussion
Copy link
Member Author

I implemented your comments with the exception of:

probably can put this in _fillna instead.

Do you mean this should/could be done in _binary_op instead?

@shoyer
Copy link
Member

shoyer commented Sep 20, 2016

probably can put this in _fillna instead.
Do you mean this should/could be done in _binary_op instead?

I forgot that we hack this into _binary_op right now. So no, please ignore this guidance then. This will cleaner after #964.

@fmaussion
Copy link
Member Author

OK. Sorry for the mess with Travis, I had a lot of typos :( It's getting late here and the new review system is a bit confusing

ds.a.attrs['attr'] = 'da'
actual = ds.groupby('b').fillna(Dataset({'a': ('b', [0, 2])}))
self.assertEqual(actual.attrs, ds.attrs)
self.assertTrue(actual.a.name == 'a')
Copy link
Member

Choose a reason for hiding this comment

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

still would be a good idea to use assertEqual here :)

Copy link
Member Author

Choose a reason for hiding this comment

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

this is what happens when one does too many things at the same time without thinking :-(

Should be ok now, hopefully...

@shoyer shoyer merged commit c1eddaf into pydata:master Sep 21, 2016
@shoyer
Copy link
Member

shoyer commented Sep 21, 2016

Thanks!

@fmaussion fmaussion deleted the keepattrs branch September 21, 2016 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants