1
我有時間戳列表:如何生成從此處,參考產生以前的時間戳每個TimeDelta一個時間戳列TimedeltaIndex
Timestamp
2015-10-01 00:00:08
2015-10-01 00:00:18
2015-10-01 00:00:28
2015-10-01 00:00:38
2015-10-01 00:00:48
2015-10-01 00:00:58
2015-10-01 00:01:08
2015-10-01 00:01:17
,我想生成時間戳,其中TimeDeltas每個TimeDelta值取基準從以前的時間戳是這樣的:
Timestamp TimeDelta
2015-10-01 00:00:08 00:00:00.000
2015-10-01 00:00:18 00:00:10.000
2015-10-01 00:00:28 00:00:10.000
2015-10-01 00:00:38 00:00:10.000
2015-10-01 00:00:48 00:00:10.000
2015-10-01 00:00:58 00:00:10.000
2015-10-01 00:01:08 00:00:10.000
2015-10-01 00:01:17 00:00:09.000
但相反我得到這個:
Timestamp TimeDelta
2015-10-01 00:00:08 00:00:00.000
2015-10-01 00:00:18 00:00:10.000
2015-10-01 00:00:28 00:00:20.000
2015-10-01 00:00:38 00:00:30.000
2015-10-01 00:00:48 00:00:40.000
2015-10-01 00:00:58 00:00:50.000
2015-10-01 00:01:08 00:01:00.000
2015-10-01 00:01:17 00:01:09.000
我用下面的代碼:
df['TimeDelta'] = pd.TimedeltaIndex(df['Timestamp'])
謝謝!
嘿感謝!我試過了,但不幸的是它告訴我'.diff()'已被棄用,並告訴我使用[.difference](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex .difference.html?highlight = difference)。我試過'df ['TimeDelta'] = pd.TimedeltaIndex(df ['date'] - df ['date']。shift(1))'及其工作! – cookiedookie
不推薦使用'DatetimeIndex.diff'方法。但是'Series.diff'方法不是。由於'df ['Timestamp']'是一系列(dtype'datetime64 [ns]'),因此使用'df ['Timestamp']。diff()'應該沒有問題。 – unutbu
你是對的!它確實有用!我犯了一個錯誤,我之前使用了以下內容:'df ['TimeDelta'] = pd.TimedeltaIndex(df ['Timestamp'])。difference()。fillna(0)'謝謝你的幫助! – cookiedookie