I have a DataArray da
with a variable called FFDI
which has three dimensions, time
, latitude
and longitude
.
xarray.DataArray 'FFDI' time: 43848 latitude: 2 longitude: 244
latitude (latitude) float32 -39.2 -39.163948
longitude (longitude) float32 140.8 140.83786 ... 149.96214 150.0
time (time) datetime64[ns] 2000-01-01T00:00:00 2000-01-01T01:00:00 ... 2004-12-31T22:00:00 2004-12-31T23:00:00
What I want to achieve is to apply the following function to each timestamp of the time
dimension to calculate if the timestamp is during the Daylight Savings period and the output a boolean.
def isDST(dt_str):
local_time_tz = pytz.timezone("Australia/Victoria")
naive_datetime = datetime.datetime.strptime (dt_str, "%Y-%m-%d %H:%M:%S")
a = local_time_tz.localize(naive_datetime)
return bool(a.dst())
The output would be an numpy array or another DataArray element; then it would be added to the original da as additional variable named isDST
.
xarray.DataArray 'FFDI' time: 43848 latitude: 2 longitude: 244
latitude (latitude) float32 -39.2 -39.163948
longitude (longitude) float32 140.8 140.83786 ... 149.96214 150.0
time (time) datetime64[ns] 2000-01-01T00:00:00 2000-01-01T01:00:00 ... 2004-12-31T22:00:00 2004-12-31T23:00:00
isDST (time) bool true true ... true true
Is this possible and what function should be used in xarray, pandas or numpy?
You can just use a simple list comprehension and add the new coordinate to the DataArray
:
import xarray as xr
import pytz
# example data
x = xr.tutorial.load_dataset("air_temperature").air
local_time_tz = pytz.timezone("Australia/Victoria")
is_dst = [bool(local_time_tz.localize(x).dst()) for x in x.time.to_index()]
x = x.assign_coords(is_dst=("time", is_dst))
# result
# Coordinates:
# * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
# * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
# * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
# is_dst (time) bool True True True True True ... True True True True True
If x
were a xarray.Dataset
instead of a xarray.DataArray
you could simply do:
x['is_dst'] = [bool(local_time_tz.localize(x).dst()) for x in x.time.to_index()]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.