I have the following list:
Index([2020-11-14 07:00:00, 2020-11-14 07:03:00, 2020-11-14 07:06:00,
2020-11-14 07:09:00, 2020-11-14 07:12:00, 2020-11-14 07:15:00,
2020-11-14 07:18:00, 2020-11-14 07:21:00, 2020-11-14 07:24:00,
2020-11-14 07:27:00,
...
2020-11-14 16:33:00, 2020-11-14 16:36:00, 2020-11-14 16:39:00,
2020-11-14 16:42:00, 2020-11-14 16:45:00, 2020-11-14 16:48:00,
2020-11-14 16:51:00, 2020-11-14 16:54:00, 2020-11-14 16:57:00,
2020-11-14 17:00:00],
dtype='object', length=201)
The type of items in the list:
type(time_lst[0])
>>>pandas._libs.tslibs.timestamps.Timestamp
I want to run over the items in my lsit and to extract only the hour and minute, to get something like this:
[07:00,07:03,07:06,07:09....]
Until now I found only posts regard how to get only the hour or the minutes separetly. I have managed to extract the hour only but I don't fund how to et the hour and the minute.
#here I get all the timestamp items inside my lsit from pandas dataframe
time_lst=tmp.columns
#get the hour
for i in time_lst:
x=i.hour
print(x)
I have trired to do something like this:
time_lst=tmp.columns
for i in time_lst:
h=i.hour
m=i.min
item=str(h)+':'+str(m)
print(item)
but this print weird values (7:1677-09-21 00:12:43.145225) and I believe there is more efficient and smart way to extract the hour and minute then convert it to string.
My end goal create new list that has the hour and minute only from my timestamps list
If need processing DatetimeIndex
use DatetimeIndex.strftime
:
L = df.index.strftime('%H:%M').tolist()
If need processing list or DatetimeIndex
use list comprehension with Timestamp.strftime
:
L = [x.strftime('%H:%M') for x in time_lst]