Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Pandas的datetime的每小时柱状图_Python_Datetime_Pandas - Fatal编程技术网

Python 使用Pandas的datetime的每小时柱状图

Python 使用Pandas的datetime的每小时柱状图,python,datetime,pandas,Python,Datetime,Pandas,假设我在pandas.DataFrame中有一个时间戳列datetime。例如,时间戳的分辨率为秒。我想用10分钟[1]桶/桶来存储事件。我知道我可以将datetime表示为整数时间戳,然后使用直方图。有没有更简单的方法?熊猫内置的什么东西 [1] 10分钟只是一个例子。最后,我希望使用不同的分辨率。要使用自定义频率,如“10Min”,您必须使用一个TimeGrouper——正如@johnchase所建议的那样——它在索引上运行 # Generating a sample of 10000 ti

假设我在
pandas.DataFrame
中有一个时间戳列
datetime
。例如,时间戳的分辨率为秒。我想用10分钟[1]桶/桶来存储事件。我知道我可以将
datetime
表示为整数时间戳,然后使用直方图。有没有更简单的方法?熊猫内置的什么东西


[1] 10分钟只是一个例子。最后,我希望使用不同的分辨率。

要使用自定义频率,如“10Min”,您必须使用一个
TimeGrouper
——正如@johnchase所建议的那样——它在
索引上运行

# 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')

使用
到\u时段
也可以使用
to_period
方法,但就我所知,对于像“10Min”这样的自定义周期,它不起作用。本例使用附加列来模拟项目的类别

# 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')

这可能会让你接近:
df.groupby(pd.TimeGrouper(freq='10Min')).mean().plot(kind=“bar”)
你可以用“hist”替换“bar”,但我不确定这是否有意义。我猜y轴应该是频率,但x轴应该是什么?你有没有原始数据的例子,以及绘图应该是什么样的(即使只是口头描述)的例子?可能的重复让我更接近了。谢谢我还有两个问题:1)x轴刻度与数据的日期时间性质无关,2)条的总和不应该是500?它不应该是
.plot(kind='bar')
而不是@johnchase建议的
.hist()
?对不起,我在第一个答案中犯了一个大错误(走得太快不是解决办法)。我刚刚编辑了它,认为它现在解决了你的问题。现在,
sum
是500:-)我实际上更喜欢
dt.to_period
的解决方案。强制索引成为时间戳是一个很大的限制