Reputation: 123
I have a list of datetime.datetime
objects:
x1 = [
datetime.datetime(2019, 8, 18, 21, 21, 23),
datetime.datetime(2019, 8, 18, 20, 38, 6),
datetime.datetime(2019, 8, 18, 18, 45, 38),
datetime.datetime(2019, 8, 18, 15, 35, 25),
datetime.datetime(2019, 8, 18, 15, 29, 54),
datetime.datetime(2019, 8, 18, 15, 26, 19)
]
And I would like to extract the day of the week they are (0 Monday, 6 Sunday, for example), and then plot 7 histogram, one per day, with the hours in the x axis.
So far no luck using .weekday()
Upvotes: 0
Views: 1701
Reputation: 6639
What you have is something like this basically, note that I have two dates here 18th August and 21st August, so two weekdays (I will not do for 7 but the logic remains the same):
x1 = [datetime.datetime(2019, 8, 18, 21, 21, 23),
datetime.datetime(2019, 8, 18, 20, 38, 6),
datetime.datetime(2019, 8, 18, 18, 45, 38),
datetime.datetime(2019, 8, 18, 15, 35, 25),
datetime.datetime(2019, 8, 18, 15, 29, 54),
datetime.datetime(2019, 8, 18, 15, 26, 19),
datetime.datetime(2019, 8, 21, 21, 21, 23),
datetime.datetime(2019, 8, 21, 21, 38, 6),
datetime.datetime(2019, 8, 21, 18, 45, 38),
datetime.datetime(2019, 8, 21, 16, 35, 25),
datetime.datetime(2019, 8, 21, 16, 29, 54),
datetime.datetime(2019, 8, 21, 15, 26, 19)]
First get all your records, hours and weekdays to a pandas dataframe:
import pandas as pd
day, hour = [], []
for x in x1:
day.append(x.weekday())
hour.append(x.hour)
df = pd.DataFrame()
df['day'] = day
df['hour'] = hour
Get the number of different days for which you have data (in your case, it will be 7, my case it is 2 from the example):
dayList = df['day'].unique().tolist()
Now plot them in a loop:
import matplotlib.pyplot as plt
for i in dayList:
tempDf = df.copy()
tempDf = tempDf[tempDf['day'] == i]
tempDf['hour'].plot(kind='hist', title='Plot for day' + str(i))
plt.show()
You will see two graphs (in your case it should be 7 graphs, one for each day) for the example I have given, it count the number of times different hours have records:
Upvotes: 3
Reputation: 1303
Maybe this is what you asked
import matplotlib.pyplot as plt
import datetime
import numpy as np
x1 = [datetime.datetime(2019, 8, 18, 21, 21, 23),
datetime.datetime(2019, 8, 18, 20, 38, 6),
datetime.datetime(2019, 8, 18, 18, 45, 38),
datetime.datetime(2019, 8, 18, 15, 35, 25),
datetime.datetime(2019, 8, 18, 15, 29, 54),
datetime.datetime(2019, 8, 18, 15, 26, 19)]
day, hours = [], []
for i in x1:
day.append(i.weekday())
hours.append(i.hour)
data = np.column_stack((hours, day))
plt.hist(data, density=1, facecolor='blue', alpha=0.8)
plt.ylabel('Day')
plt.xlabel('Hours')
plt.show()
Upvotes: 1
Reputation: 483
I can understand your question very vaguely, because I'm a newbie and couldn't think much without an example of your need. For now I think this might be helpful:
days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
x1 = [datetime.datetime(2019, 8, 18, 21, 21, 23),
datetime.datetime(2019, 8, 18, 20, 38, 6),
datetime.datetime(2019, 8, 18, 18, 45, 38),
datetime.datetime(2019, 8, 18, 15, 35, 25),
datetime.datetime(2019, 8, 18, 15, 29, 54),
datetime.datetime(2019, 8, 18, 15, 26, 19)]
for i in x1:
print(days[i.weekday()]) #This will print the day of the week with respect to the date
And using .hour
attribute of datetime object you can get the hour.
Upvotes: 0