Skip to content
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

[docs] add Dataset.assign_coords example (#6336) #6558

Merged
merged 5 commits into from
May 11, 2022
Merged
Changes from 2 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
49 changes: 48 additions & 1 deletion xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def assign_coords(self, coords=None, **coords_kwargs):

Examples
--------
Convert longitude coordinates from 0-359 to -180-179:
Convert `DataArray` longitude coordinates from 0-359 to -180-179:

>>> da = xr.DataArray(
... np.random.rand(4),
Expand Down Expand Up @@ -490,6 +490,53 @@ def assign_coords(self, coords=None, **coords_kwargs):

>>> _ = da.assign_coords({"lon_2": ("lon", lon_2)})

Note the same method applies to `Dataset` objects.

Convert `Dataset` longitude coordinates from 0-359 to -180-179:

>>> np.random.seed(0)
>>> ds = xr.Dataset(
... data_vars=dict(
... temperature=(["x", "y", "time"], 15 + 8 * np.random.randn(2, 2, 3)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

We've generally tried to not use np.random, both because of the seed issue, and because it can be difficult for people to track the numbers through the example. What do you think about replacing it with np.arange(12).reshape(2,2,3)? Or something similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions @max-sixty. I borrowed my example from the top-level xarray.Dataset examples, but your idea makes good sense. I'm quite new to xarray and wanted to keep my example similar to others already in the docs. Happy to submit another change if you think that will improve things.

Also thanks for pointing our https://github.com/max-sixty/pytest-accept. I'll take a look!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, we've been gradually going through the examples and changing them, but doc PRs are rarer than code (and so very much appreciated!), and we still have quite a few left over on random.

If you're up for making the change that would be great. If you don't think you'll get to it, this is still a good improvement. Thanks @gregbehm !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@max-sixty, I think working through the docs and adding examples will be a great way for me to learn xarray, so I'll put the change on my to-do list and try to get to it soon.

One more noob question: how do we connect this PR with the issue that prompted it, #6336 so that can get closed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That would be great!

It used to be that saying "closes #6336" worked, but I've seen that not work at times. Often someone will just manually close it after merging...

Copy link
Collaborator

Choose a reason for hiding this comment

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

It used to be that saying closes #6336 worked

this does work, but only if you use exactly that spelling in markdown. Unfortunately, github displays urls to issues / PRs the same way, hence the confusion.

If it did work, "closes" will become some kind of help link (not sure what the official name is), and on the right you see a linked issue (section "development"). In general, I think you just have to make sure the issue is linked in that section for it to work.

... precipitation=(["x", "y", "time"], 10 * np.random.rand(2, 2, 3)),
... ),
... coords=dict(
... lon=(["x", "y"], [[260.17, 260.68], [260.21, 260.77]]),
... lat=(["x", "y"], [[42.25, 42.21], [42.63, 42.59]]),
... time=pd.date_range("2014-09-06", periods=3),
... reference_time=pd.Timestamp("2014-09-05"),
... ),
... attrs=dict(description="Weather-related data"),
... )
>>> ds
<xarray.Dataset>
Dimensions: (x: 2, y: 2, time: 3)
Coordinates:
lon (x, y) float64 260.2 260.7 260.2 260.8
lat (x, y) float64 42.25 42.21 42.63 42.59
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
reference_time datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
Attributes:
description: Temperature data
>>> ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180))
<xarray.Dataset>
Dimensions: (x: 2, y: 2, time: 3)
Coordinates:
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
lat (x, y) float64 42.25 42.21 42.63 42.59
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
reference_time datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
Attributes:
description: Weather-related data

Notes
-----
Since ``coords_kwargs`` is a dictionary, the order of your arguments
Expand Down