2017-01-05 10 views
1

私は時系列データを扱っていますが、シリーズに関連付けられたタイムスタンプのシーケンスが有効かどうかを検証するための効率的な方法があるかどうかを知りたいと思います。言い換えれば、タイムスタンプのシーケンスが欠落したり値が重複していない正しい昇順であるかどうかを知りたいと思います。タイムシリーズのタイムスタンプの確認

正しい順序と重複した値が存在するかどうかを確認するのはかなり簡単ですが、タイムスタンプがないことがわかりません。

+0

:これらの差分は、タイムスタンプがを期待ように見えるかどうかを判断するために評価することができますか?あなたは効率的に尋ねました、あなたはnumpyを使用しますか? –

+0

私は、規定されたシーケンスに欠けているタイムスタンプを意味します。例えば、 –

+0

?コメントに表示されない場合は、質問を編集します。実際には質問に関係なく編集してください。それは情報のより良い場所になります。 –

答えて

1

numpy.diffは、後続のタイムスタンプの違いを見つけるために使用できます。あなたは「行方不明」とはどういう意味ですか

import numpy as np 
import datetime as dt 

def errant_timestamps(ts, expected_time_step=None, tolerance=0.02): 
    # get the time delta between subsequent time stamps 
    ts_diffs = np.array([tsd.total_seconds() for tsd in np.diff(ts)]) 

    # get the expected delta 
    if expected_time_step is None: 
     expected_time_step = np.median(ts_diffs) 

    # find the index of timestamps that don't match the spacing of the rest 
    ts_slow_idx = np.where(ts_diffs < expected_time_step * (1-tolerance))[0] + 1 
    ts_fast_idx = np.where(ts_diffs > expected_time_step * (1+tolerance))[0] + 1 

    # find the errant timestamps 
    ts_slow = ts[ts_slow_idx] 
    ts_fast = ts[ts_fast_idx] 

    # if the timestamps appear valid, return None 
    if len(ts_slow) == 0 and len(ts_fast) == 0: 
     return None 

    # return any errant timestamps 
    return ts_slow, ts_fast 


sample_timestamps = np.array(
    [dt.datetime.strptime(sts, "%d%b%Y %H:%M:%S") for sts in (
     "05Jan2017 12:45:00", 
     "05Jan2017 12:50:00", 
     "05Jan2017 12:55:00", 
     "05Jan2017 13:05:00", 
     "05Jan2017 13:10:00", 
     "05Jan2017 13:00:00", 
    )] 
) 

print errant_timestamps(sample_timestamps) 
+0

ありがとう、スティーブンしかし、時系列操作はこのような共通の課題であるため、この問題に対する事前にパッケージ化された解決策がどこかにないことがわかりました... –

+0

さて、本当にありがとうございました。プレパッケージソリューション@ https://encrypted.google.com/search?hl=ja&q=Verifying%20timestamps%20in%20a%20time%20series –