Python:Pandas 系列 - 以秒为单位的连续日期时间行之间的差异

标签 python datetime pandas difference seconds

我有一个名为“df”的 Pandas 系列,如下所示

                       value    
date_time_index         
2015-10-28 01:54:00     1.0 
2015-10-28 01:55:00     1.0 
2015-10-28 01:56:00     1.0 
2015-10-28 01:57:00     1.0 
2015-10-28 01:58:00     1.0 

我只想要一个新列,其连续行之间的差异以秒为单位,我该怎么做?

注:类型为

 type(df.index[1])

给定

 pandas.tslib.Timestamp

最佳答案

我会这样做:

df.index.to_series().diff().dt.total_seconds().fillna(0)

date_time_index
2015-10-28 01:54:00     0.0
2015-10-28 01:55:00    60.0
2015-10-28 01:56:00    60.0
2015-10-28 01:57:00    60.0
2015-10-28 01:58:00    60.0
Name: date_time_index, dtype: float64

关于Python:Pandas 系列 - 以秒为单位的连续日期时间行之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38863137/

相关文章:

sql - 如何用 Doctrine 更改日期时间值的时间部分?

python - 如何在 Pandas 中创建 lazy_evaluated 数据框列

python - 为 Pandas 条形图获取单色而不是颜色图

python - 从键码中获取字符

python - 为什么在函数conway的Python游戏中出现这种语法错误?

c# - DateTime.Now.DayOfWeek.ToString() 与 CultureInfo

mysql - SQL查询多个日期到一个日期

python - Pandas groupby 与 dict

python - 加速 sympy 矩阵运算

python - 无法使用 pip 安装 Scipy