【问题标题】:time difference between timestamps in a pandas dataframe as histogram熊猫数据帧中时间戳之间的时间差作为直方图
【发布时间】:2020-09-26 01:38:50
【问题描述】:

我有一个数据框,其中一列是所有时间戳,如下所示。我现在需要做的是计算每个时间戳之间的差异,然后使用这些差异绘制为直方图。 我无法破译如何计算差异。任何帮助将不胜感激。

0       2020-09-16 00:00:02.713264
1       2020-09-16 00:00:02.827854
2       2020-09-16 00:00:05.919288
3       2020-09-16 00:00:05.940775
4       2020-09-16 00:00:06.682184

【问题讨论】:

  • 您应该能够将差异以秒为单位绘制为直方图,例如df['timestamp'].diff().dropna().dt.total_seconds().plot.hist()
  • 谢谢。上半场效果很好。但我在情节上遇到错误 - UFuncTypeError: Cannot cast ufunc 'less' input 1 from dtype('float64') to dtype('
  • 在我原来的评论中添加了.dropna();现在应该可以工作了。
  • thanks.the dropna 可以移除 NaT 但它不会绘图。我得到了这个: UFuncTypeError: Cannot cast ufunc 'less' input 1 from dtype('float64') to dtype('

标签: python pandas dataframe datetime


【解决方案1】:

给定一个虚拟的df

# df
#                    timestamp
# 0 2020-09-16 00:00:02.713264
# 1 2020-09-16 00:00:02.827854
# 2 2020-09-16 00:00:05.919288
# 3 2020-09-16 00:00:05.940775
# 4 2020-09-16 00:00:06.682184

你应该可以使用

ax = df['timestamp'].diff().dropna().dt.total_seconds().plot.hist()
ax.set_xlabel('timedelta[s]')

...这应该产生一个像

【讨论】:

  • @GAYANSAMARAKOON:很高兴我能帮上忙。
猜你喜欢
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 2020-12-02
  • 2021-05-08
  • 2021-11-15
  • 2017-02-23
相关资源
最近更新 更多