I have a time delta that is in format numpy.timedelta(64):
value = numpy.timedelta64(30,'m')
(here 30min).
Is there a way to pass that value to the construction of a pandas date_range:
pd.date_range(start_time, end_time, freq=value)
?
If not how is it possible to convert that value in order to pass it in the date_range? I tried value.astype('str')
but it returns '0:30:00'
that is also not suited as an argument for the pd.date_range.
You can convert it to a Timedelta
and pass it into freq
.
>>> pd.date_range('20160101', '20170101', freq=pd.Timedelta(value))
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:30:00',
'2016-01-01 01:00:00', '2016-01-01 01:30:00',
'2016-01-01 02:00:00', '2016-01-01 02:30:00',
'2016-01-01 03:00:00', '2016-01-01 03:30:00',
'2016-01-01 04:00:00', '2016-01-01 04:30:00',
...
'2016-12-31 19:30:00', '2016-12-31 20:00:00',
'2016-12-31 20:30:00', '2016-12-31 21:00:00',
'2016-12-31 21:30:00', '2016-12-31 22:00:00',
'2016-12-31 22:30:00', '2016-12-31 23:00:00',
'2016-12-31 23:30:00', '2017-01-01 00:00:00'],
dtype='datetime64[ns]', length=17569, freq='30T')
I think older versions of pandas
didn't support this, but could still use a frequency string:
pd.date_range('20160101', '20170101', freq='30min')
Since you are starting with a numpy.timedelta64
, I wondered how np.arange
handled this.
With datetime64
start and stop values arange
handles this nicely:
In [847]: x=np.arange(np.datetime64('2016-01-01'), np.datetime64('2017-01-01'),value)
In [848]: x
Out[848]:
array(['2016-01-01T00:00', '2016-01-01T00:30', '2016-01-01T01:00', ...,
'2016-12-31T22:30', '2016-12-31T23:00', '2016-12-31T23:30'], dtype='datetime64[m]')
and DatetimeIndex
accepts such an array:
In [849]: pd.DatetimeIndex(x)
Out[849]:
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:30:00',
'2016-01-01 01:00:00', '2016-01-01 01:30:00',
'2016-01-01 02:00:00', '2016-01-01 02:30:00',
'2016-01-01 03:00:00', '2016-01-01 03:30:00',
'2016-01-01 04:00:00', '2016-01-01 04:30:00',
...
'2016-12-31 19:00:00', '2016-12-31 19:30:00',
'2016-12-31 20:00:00', '2016-12-31 20:30:00',
'2016-12-31 21:00:00', '2016-12-31 21:30:00',
'2016-12-31 22:00:00', '2016-12-31 22:30:00',
'2016-12-31 23:00:00', '2016-12-31 23:30:00'],
dtype='datetime64[ns]', length=17568, freq=None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With