Reputation: 23
I'm kind of new in python. I have an xarray
DataArray which contains a variable made of floats. These floats are supposed to be seconds. I would like to add these floats to a given date:
2021-01-01 00:00:00 + 1 = 2021-01-01 00:00:01
...
2021-01-01 00:00:00 + 120 = 2021-01-01 00:02:00
in order have an array of datetime I can work with (for example to extract the events between a given interval of time)
I tried to use timedelta but it does not like DataArray. Is there a workaround for that?
Upvotes: 2
Views: 766
Reputation: 2097
For standard calendar operations, xarray works best with NumPy datetime64
and timedelta64
types. In your particular case, I would recommend converting your DataArray of floats to timedelta64
(taking into account the units of seconds), and adding those values to a datetime64
instance. This will produce a DataArray of type datetime64[ns]
, which you can use in other operations:
In [1]: import numpy as np; import xarray as xr
In [2]: data = np.arange(10.)
In [3]: da = xr.DataArray(data, dims=["x"])
In [4]: np.datetime64("2000-01-01") + da.astype("timedelta64[s]")
Out[4]:
<xarray.DataArray (x: 10)>
array(['2000-01-01T00:00:00.000000000', '2000-01-01T00:00:01.000000000',
'2000-01-01T00:00:02.000000000', '2000-01-01T00:00:03.000000000',
'2000-01-01T00:00:04.000000000', '2000-01-01T00:00:05.000000000',
'2000-01-01T00:00:06.000000000', '2000-01-01T00:00:07.000000000',
'2000-01-01T00:00:08.000000000', '2000-01-01T00:00:09.000000000'],
dtype='datetime64[ns]')
Dimensions without coordinates: x
Upvotes: 2
Reputation: 7
This is for time:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(current_time)
This is for date:
from datetime import date
today = date.today()
print("Today's date:", today)
Upvotes: -1