如何在熊猫数据框中找到两个时间戳之间的差异

问题描述

我有这个数据帧,在这里我想找出每一行的时差

ax = sns.FacetGrid(telcom,hue='churn',palette=["teal","crimson"],size=5,aspect=1)
ax = ax.map(sns.distplot,"tenure",hist=True,kde=False)
ax.fig.suptitle('Tenure distribution in customer churn',y=1,fontsize=16,fontweight='bold');
plt.legend();

我尝试了以下代码

      open     close
0   09:44:00  10:07:00
1   10:07:00  11:01:00
2   11:05:00  13:05:00

但是发生了以下错误

import numpy as np
(df['open']-df['close'])/np.timedelta64(1,'m')

请帮助我!

解决方法

您必须使用此功能。

pd.Timedelta(df['open']-df['close']).seconds

结果以秒为单位

,

您可以在减号之前使用pd.to_datetime()转换值:

print(
    (pd.to_datetime(df['close']) - pd.to_datetime(df['open'])) / np.timedelta64(1,'m')
)

打印:

0     23.0
1     54.0
2    120.0
dtype: float64