[英]xarray - apply a function to the time dimension of a DataArray and add the result as a variable
I have a DataArray da
with a variable called FFDI
which has three dimensions, time
, latitude
and longitude
.我有一个 DataArray
da
,其中有一个名为FFDI
的变量,它具有三个维度, time
, latitude
和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.我想要实现的是将以下 function 应用于
time
维度的每个时间戳,以计算时间戳是否在夏令时期间以及 output 和 Z84E2C64F38F78BA3EAZ5C905AB5A2DA27。
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; output 将是 numpy 数组或另一个 DataArray 元素; then it would be added to the original da as additional variable named
isDST
.然后它将作为名为
isDST
的附加变量添加到原始 da 中。
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?这可能吗?应该在 xarray、pandas 或 numpy 中使用什么 function?
You can just use a simple list comprehension and add the new coordinate to the DataArray
:您可以只使用简单的列表推导并将新坐标添加到
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
是xarray.Dataset
而不是xarray.DataArray
你可以简单地做:
x['is_dst'] = [bool(local_time_tz.localize(x).dst()) for x in x.time.to_index()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.