[英]Calculate Pandas df timedelta of index
Would anyone know how to calculate the time delta of the time stamp of the index?有谁知道如何计算指数时间戳的时间增量?
import pandas as pd
import numpy as np
# simulate some data
# ===================================
np.random.seed(0)
dt_rng = pd.date_range('2015-03-02 00:00:00', '2015-07-19 23:00:00', freq='T')
dt_idx = pd.DatetimeIndex(np.random.choice(dt_rng, size=2000, replace=False))
df = pd.DataFrame(np.random.randn(2000), index=dt_idx, columns=['col']).sort_index()
df
Am I on track using df['elapsed_time'] = pd.TimedeltaIndex(df)
at all with this ? am我使用轨道
df['elapsed_time'] = pd.TimedeltaIndex(df)
在所有与此?
This will throw an error: ValueError: Wrong number of items passed 2000, placement implies 1
这将抛出一个错误:
ValueError: Wrong number of items passed 2000, placement implies 1
This answer is beautiful! 这个答案很漂亮!
This will create another pandas column which I called time_td
where then I can cast it as a timedelta64
where m
stands for minutes which I am looking for.这将创建另一个我称之为
time_td
列,然后我可以将它转换为timedelta64
,其中m
代表我正在寻找的分钟。
df['time_td'] = df.index.to_series().diff().astype('timedelta64[m]')
I can then sum this time_td
column with:然后我可以将这个
time_td
列与:
df.time_td.sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.