2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

numpy.timedelta64で時刻データをチェック

Posted at

例えば5万行の温度データは10分間隔で収集しているつもりだが, 本当に時刻カラムは10分間隔なのかをチェックしたいので, numpy.timedelta64()関数を使ってチェックすることができる.

import numpy as np
import pandas as pd

df = pd.DataFrame({
            '時刻': pd.to_datetime(['2017-08-01 01:00:00', '2017-08-01 01:10:00', '2017-08-01 01:20:00', '2017-08-01 01:40:00'])
            ,'温度': [21, 22, 23,24]
})
Screen Shot 2017-08-05 at 9.47.26.png

i+1個目とi個目の時刻の差は<class 'pandas._libs.tslib.Timedelta'> 型で,下のようなフォーマットになっている.

0 days 00:10:00
0 days 00:10:00
0 days 00:20:00

時刻の差分を (delta_tmp / np.timedelta64(1, 'm')).astype(int)で計算して,整数に変換する.

for i in list(range(len(df['時刻'])-1)):
    delta_tmp = df['時刻'][i+1] - df['時刻'][i]

    # 時刻の差分は何分あるかを計算してintに変換
    delta_final = (delta_tmp / np.timedelta64(1, 'm')).astype(int)

    if delta_final != 10:
        print(i+1, delta_final)

これで10分間隔のデータではないときに見つかるはず.

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?