To use custom frequency like "10Min" you have to use a TimeGrouper
-- as suggested by @johnchase -- that operates on the index
.
# Generating a sample of 10000 timestamps and selecting 500 to randomize them
df = pd.DataFrame(np.random.choice(pd.date_range(start=pd.to_datetime('2015-01-14'),periods = 10000, freq='S'), 500), columns=['date'])
# Setting the date as the index since the TimeGrouper works on Index, the date column is not dropped to be able to count
df.set_index('date', drop=False, inplace=True)
# Getting the histogram
df.groupby(pd.TimeGrouper(freq='10Min')).count().plot(kind='bar')
Using to_period
It is also possible to use the to_period
method but it does not work -- as far as I know -- with custom period like "10Min". This example take an additional column to simulate the category of an item.
# The number of sample
nb_sample = 500
# Generating a sample and selecting a subset to randomize them
df = pd.DataFrame({'date': np.random.choice(pd.date_range(start=pd.to_datetime('2015-01-14'),periods = nb_sample*30, freq='S'), nb_sample),
'type': np.random.choice(['foo','bar','xxx'],nb_sample)})
# Grouping per hour and type
df = df.groupby([df['date'].dt.to_period('H'), 'type']).count().unstack()
# Droping unnecessary column level
df.columns = df.columns.droplevel()
df.plot(kind='bar')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…