2016-07-07 11 views
0

タイムスタンプhh-mm-ss形式のCSVファイルにセンサーデータ(加速度計、コンパス)が保存されています。タイムスタンプを秒に変換して、コンパスの読みを特定の秒でプロットしたかったのです。 例: 私はPythonを使用してCSVからタイムスタンプを秒単位に変換する

11-26-32 -> 0 sec 
11-26-33 -> 1 sec 
11-26-34 -> 2 sec 

を変換したいです。 。 。 私はx軸上の秒とy軸上のコンパスの向きで読み取り値をプロットすることができます。事前

答えて

0

おかげdatetimeオブジェクトに時間を変換し、秒数を見つけるために、最初の時からdatetimeオブジェクトの違いを使用します。 total_secondstimedeltaの秒数を返します。

from datetime import datetime as dt 

times = ['11-26-32', '11-26-33', '11-26-34'] 
time_format = '%H-%M-%S' 

base_time = dt.strptime(times[0], time_format) 
seconds = [(dt.strptime(t, time_format)- base_time).total_seconds() for t in times] 
print(seconds) 
# [0.0, 1.0, 2.0] 

私は、彼らが提供されていなかったので、タイムスタンプの年、月と日がすべて同じであると仮定します。

0

パンダソリューション:

次のCSVファイルを持っていると仮定:

Time,val 
11-26-32,11 
11-26-33,31 
11-26-34,33 
11-26-35,10 
11-26-39,7 

ソリューション:今

import pandas as pd 

In [225]: filename = r'C:\Temp\.data\a.csv' 

In [226]: df = pd.read_csv(filename) 

In [227]: df 
Out[227]: 
     Time val 
0 11-26-32 11 
1 11-26-33 31 
2 11-26-34 33 
3 11-26-35 10 
4 11-26-39 7 

In [228]: df.Time = pd.to_datetime(df.Time, format='%H-%M-%S') 

In [229]: df['sec'] = (df.Time - df.ix[0, 'Time']).dt.seconds 

In [230]: df 
Out[230]: 
       Time val sec 
0 1900-01-01 11:26:32 11 0 
1 1900-01-01 11:26:33 31 1 
2 1900-01-01 11:26:34 33 2 
3 1900-01-01 11:26:35 10 3 
4 1900-01-01 11:26:39 7 7 

のは、それをプロットしてみましょう:

In [235]: df.set_index('sec').plot() 
Out[235]: <matplotlib.axes._subplots.AxesSubplot at 0x808e2b0> 

enter image description here

関連する問題