Using datetime / timedelta in Xarray / Pandas

PHOTO EMBED

Wed Feb 23 2022 19:22:03 GMT+0000 (Coordinated Universal Time)

Saved by @ktyle #python

# Assume we have a data array that has a time dimension, e.g. forecast_initial_time, and
# we want to compute a new time via timedelta:

# in this case, timeRange is an array of initial forecast times
for x in np.arange(0,len(timeRange),1):  
  for y in range(1,13):
    # Add the forecast_hour to the forecast_initial_time to determine the valid time
    valTime = fh.forecast_initial_time[x] + pd.Timedelta(hours=y)
    print(pd.to_datetime(valTime.values,format="%Y%m%d/%H%M UTC"))
content_copyCOPY