Reputation: 91
Im trying to generate a numpy array of minutes from 9:30 to 16:00. (6.5 hours*60 minutes=390 elements)
Is there something similar to:
import pandas as pd
pd.date_range("09:30", "16:00", freq="1min")
Of course after it to convert to numpy is easy...
Upvotes: 3
Views: 1755
Reputation: 53089
You could use np.datetime64
. But it will insist on being given a year-month-day, as well.
r = np.arange('0000-01-01T09:30', '0000-01-01T16:00', dtype='M8')
r
# array(['0000-01-01T09:30', '0000-01-01T09:31', '0000-01-01T09:32',
# '0000-01-01T09:33', '0000-01-01T09:34', '0000-01-01T09:35',
# ...
# '0000-01-01T15:54', '0000-01-01T15:55', '0000-01-01T15:56',
# '0000-01-01T15:57', '0000-01-01T15:58', '0000-01-01T15:59'],
# dtype='datetime64[m]')
You could get rid of the date by subtracting it but the resulting np.timedelta64
does display as minutes.
d = r - r[0].astype('M8[D]')
d
# array([570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582,
# 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595,
# ...
# 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946,
# 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959],
# dtype='timedelta64[m]')
Hours can be extracted, though:
>> d.astype('m8[h]')
# array([ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
# 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
# ...
# 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
# 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15],
# dtype='timedelta64[h]')
Upvotes: 0
Reputation: 26906
There is nothing that high-level for date/time management in numpy
, but it is not too difficult to have your own with the help of the datetime
standard module.
One way of doing this would be:
import datetime
def date_range(
begin_time,
end_time,
step_time,
in_date_fmt='%H:%M',
out_date_fmt=None,
upper_bound=False):
if out_date_fmt is None:
out_date_fmt = in_date_fmt
begin_time = datetime.datetime.strptime(begin_time, in_date_fmt)
end_time = datetime.datetime.strptime(end_time, in_date_fmt)
delta_time = (end_time - begin_time)
origin_time = datetime.datetime.strptime('0', '%S')
step_time = (
datetime.datetime.strptime(step_time, in_date_fmt) - origin_time)
if upper_bound:
upper_bound = step_time.seconds
for i in range(0, delta_time.seconds + upper_bound, step_time.seconds):
yield (
begin_time +
datetime.timedelta(seconds=i)).strftime(out_date_fmt)
Which can be used this way:
my_date_range = np.array(list(date_range('09:30', '16:00', '00:01')))
The docstring is left as an exercise for the reader ;-)
Of course, if you can use pandas
you should really use it and not invent Yet Another Wheel™.
Upvotes: 0
Reputation: 863291
I believe you need DatetimeIndex.strftime
:
a = pd.date_range("09:30", "16:00", freq="1min").strftime('%H:%M')
print (a[:10])
['09:30' '09:31' '09:32' '09:33' '09:34' '09:35' '09:36' '09:37' '09:38'
'09:39']
Upvotes: 4
Reputation: 433
Try this:
t = np.arange(datetime.datetime(2018,3,12,9,30), datetime.datetime(2018,3,12,16,0), datetime.timedelta(hours=1)).astype(datetime.datetime)
Upvotes: 0