Skip to content

DOC: add interp example #2312

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 3 commits into from
Jul 28, 2018
Merged

DOC: add interp example #2312

merged 3 commits into from
Jul 28, 2018

Conversation

raybellwaves
Copy link
Contributor

  • [NA] Closes #xxxx (remove if there is no corresponding issue, which should only be the case for minor changes)
  • [NA] Tests added (for all bug fixes or enhancements)
  • [NA] Tests passed (for all non-documentation changes)
  • [NA] Fully documented, including whats-new.rst for all changes and api.rst for new API (remove if this change should not be visible to users, e.g., if it is an internal clean-up, or if this is part of a larger project that will be documented later)

I wanted to add an example as googling 'xarray interp' takes me to http://xarray.pydata.org/en/stable/generated/xarray.Dataset.interp.html.

However, there lots of great examples in http://xarray.pydata.org/en/stable/interpolation.html
Perhaps I should edit it to:

Examples
--------
See http://xarray.pydata.org/en/stable/interpolation.html

I was also thinking about adding a note about interpolating time as it came up in #2284 but not sure what would be the best way to do so.

@fujiisoup
Copy link
Member

Thanks for adding this to doc.
It is very welcome if you could add an example for interp with datetime.

To do so, it would be best place to put it in here
https://github.com/pydata/xarray/blob/master/doc/interpolation.rst

@raybellwaves
Copy link
Contributor Author

@fujiisoup I would like to add another example on how to update a DataArray with the interpolated data. I looked in http://xarray.pydata.org/en/stable/combining.html but couldn't work out how best to do it.

I tried using merge but it requires the DataArray's to be named.

I used concat in this example but it is not correct as I would like the coordinates to be in order (in-place may be the right word).

import xarray as xr
import pandas as pd
import numpy as np

da_dt64 = xr.DataArray([1, 3], [('time', pd.date_range('1/1/2000', '1/3/2000', periods=2))])
a = da_dt64.interp(time=np.datetime64('2000-01-02'))
xr.concat([da_dt64, a], dim='time')
#<xarray.DataArray (time: 3)>
#array([ 1.,  3.,  2.])
#Coordinates:
#  * time     (time) datetime64[ns] 2000-01-01 2000-01-03 2000-01-02

@fujiisoup
Copy link
Member

Thanks.

I think you can do

da_dt64.interp(time=pd.date_range('1/1/2000', '1/3/2000', periods=3))

@@ -48,6 +48,23 @@ array-like, which gives the interpolated result as an array.
# interpolation
da.interp(time=[2.5, 3.5])


To interpolate data with a :py:func:`numpy.datetime64` coordinate you have to
wrap it explicitly in a :py:func:`numpy.datetime64` object.
Copy link
Member

Choose a reason for hiding this comment

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

It is currently true, but I think we could support da_dt64.interp(time='2000-01-02') in the future.
Do you mind to send another PR to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm trying to wrap up another PR. Unfortunately, I don't want to over promise and may not be able to get to this until a couple of weeks. If you have ideas of how to do this we can move the discussion to #2284

@fujiisoup fujiisoup merged commit 2fa9dde into pydata:master Jul 28, 2018
@fujiisoup
Copy link
Member

Thanks, @raybellwaves.

fujiisoup added a commit to fujiisoup/xarray that referenced this pull request Jul 28, 2018
fujiisoup added a commit that referenced this pull request Jul 28, 2018
dcherian pushed a commit to dcherian/xarray that referenced this pull request Jul 28, 2018
* master:
  Add libraries to show_versions (pydata#2321)
  fix doc build error after pydata#2312 (pydata#2326)
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