簡體   English   中英

使用Pandas僅移動DatetimeIndex系列的一部分並將其放回到向前移動的文件中

[英]Using Pandas to shift only a slice of a DatetimeIndex series and put it back in to the file shifted ahead

我的時間序列存在DST問題。 簡而言之,我想拉出該系列的一部分,並將其向前移動一個小時(分鍾數據),然后再放回已移動的切片,並用0填充所有缺失的值。

這是我需要轉移一個小時的數據:

2014,345,0744,0,0
2014,345,0745,0,0
2014,345,0746,0,0
2014,345,0747,0,.018
2014,345,0748,.052,.215
2014,345,0749,.155,.268
2014,345,0750,.172,.375
2014,345,0751,.396,.429
2014,345,0752,.413,.447

這是我需要上述內容的示例(提前一個小時):

2014,344,0844,0,0
2014,344,0845,0,0
2014,344,0846,0,0
2014,344,0847,0,.054
2014,344,0848,0,.107
2014,344,0849,.138,.197
2014,344,0850,.189,.125
2014,344,0851,.276,.179
2014,344,0852,.155,.143
2014,344,0853,.103,.161

我的方法是將需要將數據移位一個小時的時間片切成薄片,將數據移位一個小時,然后用移位的片段替換原始數據。

到目前為止,我已經嘗試過了:

slice = df['20141211 02:00':'20141227 22:00'] #The slice needing to be shifted

shifted = slice.shift(periods=1, freq='60T') #Move the datetime values forward an hour

df.merge(shifted, left_index=True, right_index=True)

切片和移位工作正常,但是最后一行代碼沒有完成我想要的操作。 我也嘗試了left_index和left_on的所有變體...我得到的最接近的是上面給我的東西:

    3_x 4_x 3_y 4_y
datetime                
2014-12-11 07:30:00 0.000   0.000   0.000   0.000
2014-12-11 07:31:00 0.000   0.000   0.000   0.000
2014-12-11 07:32:00 0.000   0.000   0.000   0.000
2014-12-11 07:33:00 0.000   0.000   0.000   0.000
2014-12-11 07:34:00 0.000   0.000   0.000   0.000
2014-12-11 07:35:00 0.000   0.000   0.000   0.000
2014-12-11 07:36:00 0.000   0.000   0.000   0.000
2014-12-11 07:37:00 0.000   0.000   0.000   0.000
2014-12-11 07:38:00 0.000   0.000   0.000   0.000
2014-12-11 07:39:00 0.000   0.000   0.000   0.000
2014-12-11 07:40:00 0.000   0.000   0.000   0.000
2014-12-11 07:41:00 0.000   0.000   0.000   0.000
2014-12-11 07:42:00 0.000   0.000   0.000   0.000
2014-12-11 07:43:00 0.000   0.000   0.000   0.000
2014-12-11 07:44:00 0.000   0.000   0.000   0.000
2014-12-11 07:45:00 0.000   0.000   0.000   0.000
2014-12-11 07:46:00 0.000   0.000   0.000   0.000
2014-12-11 07:47:00 0.000   0.018   0.000   0.000
2014-12-11 07:48:00 0.052   0.215   0.000   0.000
2014-12-11 07:49:00 0.155   0.268   0.000   0.000
2014-12-11 07:50:00 0.172   0.375   0.000   0.000
2014-12-11 07:51:00 0.396   0.429   0.000   0.000
2014-12-11 07:52:00 0.413   0.447   0.000   0.000

請注意,篩選是如何工作的,並添加了另外幾列,但是我想用shifted(右邊的2)替換原始列(df,左邊)。 要在8:47而非7:47左右開始值,即:

2014-12-11 08:44:00 6.338   7.080   0.000   0.000
2014-12-11 08:45:00 7.170   8.030   0.000   0.000
2014-12-11 08:46:00 7.720   8.150   0.000   0.000
2014-12-11 08:47:00 6.959   7.780   0.000   0.018
2014-12-11 08:48:00 6.166   6.991   0.052   0.215
2014-12-11 08:49:00 5.029   6.061   0.155   0.268
2014-12-11 08:50:00 4.444   5.632   0.172   0.375

經過多次試驗和錯誤,我發現了一種方法,該方法可以從按日期時間索引的數據幀中刪除切片,將其移動一個小時,然后將其組合回原始數據幀中。 感謝aux_lacy的幫助。 見下文:

In [153]:

slice = df['20141211 02:00':'20141227 22:00'] # The chunk to slice out and shift
In [154]:

shifted = slice.shift(periods=1, freq='60T') # The shift ahead an hour (minutely data)
In [162]:

df = shifted.combine_first(df) # combine the 2 data frames (luckily the values needed to be zeros anyway, so not sure if that would have bee something else to consider, but in this case it worked!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM