Skip to content

Feature request: How Do I add a new variable to a data set? #3678

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

Closed
rpgoldman opened this issue Jan 9, 2020 · 5 comments · Fixed by #3679
Closed

Feature request: How Do I add a new variable to a data set? #3678

rpgoldman opened this issue Jan 9, 2020 · 5 comments · Fixed by #3679

Comments

@rpgoldman
Copy link
Contributor

A common thing to want to do is to create a new DataArray and add it to an existing Dataset, but the answer does not seem to appear in the "How Do I?" list.

Thanks!

@dcherian
Copy link
Contributor

dcherian commented Jan 9, 2020

ds["name"] = data_array Also see Dataset.assign

A pull request would be very welcome!

@rpgoldman
Copy link
Contributor Author

OK, done. Per my comments, I don't have the ability to build and check the sphinx code, so please check before merging.

@rpgoldman
Copy link
Contributor Author

@dcherian
I know it's kind of overloading this issue, but if you would also LMK if there's a clean way to do compound querying, the way one can in Pandas, I will also put in something about that.

foo = data_array.where(np.logical_and(other_data_array['bar'] == 12, other_data_array['baz'] < 22))

...is all I have come up with and this gets bloated and unreadable really quickly if there are multiple conjuncts. Infix conjunction seems not to work unless I take the .values of the data array subsets, which adds its own layer of bloat.

I figure this is something that lots of people will want to do, but it seems a lot harder than pd.DataFrame.query()

@mathause
Copy link
Collaborator

You can shorthen this using parantheses and &:

sel = (other_data_array['bar'] == 12) & (other_data_array['baz'] < 22)
foo = data_array.where(sel)

@rpgoldman
Copy link
Contributor Author

@mathause Thank you! That never occurred to me.

I thought it was something wrong with the way I was using conjunction, but now I believe it was just a matter of operator precedence.

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 a pull request may close this issue.

3 participants