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

time dimension #1334

Closed
Marston opened this issue Mar 28, 2017 · 9 comments
Closed

time dimension #1334

Marston opened this issue Mar 28, 2017 · 9 comments
Labels

Comments

@Marston
Copy link

Marston commented Mar 28, 2017

Hi,

I'm new to xarray and would like to use it to process some data. For this task I need to access the time dimension and get the date in terms of year, month, and hour. I can do this if the time variable is a netcdf object but xarray does not return this object type.

For example:

import netcdf4 as nc

idata = nc.Dataset(ifile,'r')
time = idata.variables['time']
dtime = num2date(time[:],time.units)
hour = time[0].hour

How could access the time sub-variables in a similar manner?

@fmaussion
Copy link
Member

Yes, many new users of xarray are confused by this one: ds['time.hour'] is probably what you want. It is documented here:

http://xarray.pydata.org/en/latest/time-series.html#datetime-components

@Marston
Copy link
Author

Marston commented Mar 28, 2017

Aha! I see, but how do I access the data as an integer? I apologise, I'm new and it is not clear from the link how to access this as standard types:

hour.time
<xarray.DataArray 'time' ()>
array(1167620400000000000, dtype='datetime64[ns]')
Coordinates:
time datetime64[ns] 2007-01-01T03:00:00
Attributes:
standard_name: time
long_name: time
axis: T

hour.time give the same as above.

@fmaussion
Copy link
Member

I don't really understand what you mean. Here is the example from the docs again:

import xarray as xr
import pandas as pd
import numpy as np
time = pd.date_range('2000-01-01', freq='H', periods=24)
ds = xr.Dataset({'foo': ('time', np.arange(24)), 'time': time})
ds['time.hour']

which prints:

<xarray.DataArray 'hour' (time: 24)>
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23], dtype=int32)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-01T01:00:00 ...

What is it exactly what you are expecting?

@Marston
Copy link
Author

Marston commented Mar 28, 2017

apologies for these rookie questions. What I want is to access the array [0,1,2,3....] as integers, for example. Hope this is a bit clearer. I'm just checking if xarray can be use within my processing chain. Perhaps xr is it something more to plot data, and the objects within xr are only capable of manipulation only within a limited framework.

You can close this issue. I can ask this on stackoverflow as this is not truly a bug or the like.

@fmaussion
Copy link
Member

What I want is to access the array [0,1,2,3....] as integers

ds['time.hour'].values gives you access to the underlying numpy arrays. However, it is needed only in rare case since xarray data structures behave like numpy arrays (one reason why you would want to access the numpy arrays instead of xarrays DataArrays is for performance).

Perhaps xr is it something more to plot data, and the objects within xr are only capable of manipulation only within a limited framework.

I strongly disagree with that one of course ;-) Let us now if we can do anything about the documentation in order to improve it.

@Marston
Copy link
Author

Marston commented Mar 28, 2017

Ok. Concrete examples goes a far way. Maybe including this example into the docs might clarify how a user might access the data. I will use xr in the later stages of my work to analyse the data.
Appreciate the clarification :-)

@rabernat
Copy link
Contributor

I think this is related to #1282: docs frequently assume users are fluent pandas users, which is not always the case.

@Marston
Copy link
Author

Marston commented Mar 28, 2017

This is true. I find that the Pandas creators assume too many things about the user. Not wanting to imply dumbing down, but users in science come to a module with a specific problem in mind, not to learn the module from scratch. Most of us are quick learners that can handle a steep learning curve if the docs/examples are rich in relevant info (intensive), which need not be long and drawn out. It would have been a better assumption to think that users such as myself are familiar with netCDF4, HDF4, Numpy, rather than the younger modules such as dask, xarray, and pandas, and draw associations from there.

@stale
Copy link

stale bot commented Feb 26, 2019

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Feb 26, 2019
@stale stale bot closed this as completed Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants