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

convert xarray dataset to local timezone #1036

Closed
slharris opened this issue Oct 4, 2016 · 2 comments
Closed

convert xarray dataset to local timezone #1036

slharris opened this issue Oct 4, 2016 · 2 comments

Comments

@slharris
Copy link

slharris commented Oct 4, 2016

Can I convert an xarray dataset to a different timezone?
I have tried using similar steps that I would use in pandas to convert from UTC to 'Australia/Sydney'. I have pasted below some of these steps, along with a small section of the dataset I am working with.
Any feedback will be greatly appreciated.

ds = xray.open_mfdataset('/DATA/WRF///*T_SFC.nc')

import pytz
ds_utc = ds['time'].tz_localize(pytz.UTC)
au_tz = pytz.timezone('Australia/Sydney')
ds_local = ds_utc.astimezone(au_tz)

<xray.Dataset>
Dimensions: (latitude: 106, longitude: 193, time: 17520)

Coordinates:

  • latitude (latitude) float32 -39.2 -39.1495 -39.099 -39.0486 -38.9981 ...
  • longitude (longitude) float32 140.8 140.848 140.896 140.944 140.992 ...
  • time (time) datetime64[ns] 2009-01-01 2009-01-01T01:00:00 ...

Data variables:
T_SFC (time, latitude, longitude) float64 13.83 13.86 13.89 13.92 ...

Attributes:
creationTime: 1431922712
creationTimeString: Sun May 17 21:18:32 PDT 2015
Conventions: COARDS

@shoyer
Copy link
Member

shoyer commented Oct 4, 2016

Do any of the tz_localize or astimezone lines work? If so, that would surprise me. We do not have any methods related to timezone support in xarray.

That said, it should probably work (not tested) to manipulate ds['time'].to_index() as a pandas Index and then save it back in an xarray Dataset.

@slharris
Copy link
Author

slharris commented Oct 5, 2016

Thank you - ds['time'].to_index() seems to work but I am having trouble replacing the UTC time in the ds with the converted local time in my new ds. I am using .update() because I want to override the original time. However, the time does not seem to have been changed following this step.

import pandas as pd
import xray
import pytz

ds = xray.open_mfdataset('/DATA/WRF///*T_SFC.nc')

time=ds['time'].to_index()

time_utc = time.tz_localize(pytz.UTC)
au_tz = pytz.timezone('Australia/Sydney')
time_local = time_utc.tz_convert(au_tz)

#convert pandas datetimeindex to a series and then series to xarray dataarray and then to xray dataset
local_series=time_local.to_series()
local_da=xray.DataArray.from_series(local_series)
local_ds=xray.DataArray.to_dataset(local_da)

#create a new dataset that replaces/updates the UTC time in the original dataset with the local time from the newly created dataset (in pandas)
new_ds=ds.update(local_ds)

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

No branches or pull requests

2 participants