python:timedelta值的直方图
我有带有['time\u start','time\u end']信息的df,其列为datetime类型。添加新列:python:timedelta值的直方图,python,matplotlib,histogram,timedelta,Python,Matplotlib,Histogram,Timedelta,我有带有['time\u start','time\u end']信息的df,其列为datetime类型。添加新列: df['period'] = df['time_end'] - df['time_start'] 然后,我想了解“周期”的柱状图,并尝试绘制图表: plt.hist(df['period'], color = 'cyan', edgecolor = 'black', bins = int(180/10)) plt.show() 并将错误返回: UFuncTypeError:
df['period'] = df['time_end'] - df['time_start']
然后,我想了解“周期”的柱状图,并尝试绘制图表:
plt.hist(df['period'], color = 'cyan', edgecolor = 'black', bins = int(180/10))
plt.show()
并将错误返回:
UFuncTypeError: Cannot cast ufunc 'less' input 1 from dtype('float64') to dtype('<m8[ns]') with casting rule 'same_kind'
它也很好用
请您解释一下如何分析时间增量数据好吗?您能展示一下df.period.head()的样子吗?它可能很简单,比如需要首先转换为浮点:
(df.time\u end-df.time\u start)。astype(float)
?没有一个可复制的例子,我很难做到sure@MichaelSilverstein这里是:0 0天01:21:20.034990 1 0天19:39:04.498480 2 5天21:33:43.898156 3 0天14:15:47.531027 4 0天15:17:58.905265 Name:rent_time,dtype:timedelta64[ns]关于你的代码,我建议使用.total_seconds()
,,不仅是.seconds
。除此之外,“如何分析timedelta数据”并不是一个真正的编程问题(=离题)…@MrFuppes谢谢!
df['period'] = (df['time_end'] - df['time_start']).dt.seconds