I need to plot campaign data using matplotlib. The time sent out needs to be compared between different camapigns running on the same day, so only the HH:MM
is important, and that's provided in a separate column.
I receive the data in csv, with one column showing the times, unfortunately if it was sent out at eg 09:45
, it apperars as 9:45
:
['9:05', '9:20', '9:25', '9:35', '9:40', '10:17', '10:22'...]
Can anyone please give me hint? I use Python 3, really beginer user, so believe soemone could point out my obvious mistake and direct me to the right solution. Thanks
import pandas
from datetime import datetime
from datetime import time
file_name = r'''C:\\Users\\testing.csv''' # name of your excel file
df1 = pandas.read_csv(file_name, encoding='utf-8')
df_Campaign1 = df1[df1['DataSource ID'].str.contains('Campaign1')==True]
Campaign1_times = df_Campaign1['time sent'].tolist()
Campaign1_times = [datetime.strptime(slot,"%H:%M") for slot in Campaign1_times]
print(Campaign1_times)
I expected to receive 09:05
etc datetime format so I can plot multiple campaigns results against each other on a timeline but unfortunately the output is:
[datetime.datetime(1900, 1, 1, 9, 5), datetime.datetime(1900, 1, 1, 9, 20), datetime.datetime(1900, 1, 1, 9, 25), datetime.datetime(1900, 1, 1, 9, 35), datetime.datetime(1900, 1, 1, 9, 40), datetime.datetime(1900, 1, 1, 10, 17)...]
Thi is probably far from the best solution, but the following code returned the time in a HH:MM:SS format. Is this what you need?
import datetime
time_ = str(datetime.datetime.strptime('9:30', '%H:%M').time())
print(time_)
The example returns the following string: '09:30:00'