Pandasで時系列データのデータフレームの作成とプロット

こういうCSVデータを時系列データとして扱いたい。
Timeデータは(秒)を表す。

Time,Temperature,Humidity,CO2,"Soil Moisture"
22,20.7,79,380,80
44,20.7,80,380,87
66,20.7,80,396,88
88,20.6,79,399,87
110,20.6,79,399,87
132,20.6,79,399,87
154,20.6,79,396,87
176,20.6,79,399,87
198,20.6,79,399,88

任意時刻の取得は以下で行う。
stackoverflow.com

>>> from datetime import datetime
>>> datetime.strptime("2012-may-31 19:00", "%Y-%b-%d %H:%M")
 datetime.datetime(2012, 5, 31, 19, 0)

任意時刻のPOSIX時間への変換は以下で行う。
www.geeksforgeeks.org

>>> import datetime
>>> import time
>>> date_time = datetime.datetime(2021, 7, 26, 21, 20)
>>> print("unix_timestamp => ",
      (time.mktime(date_time.timetuple())))

1627314600.0

POSIX秒から時刻への変換は以下で行う。
stackoverflow.com

>>> from datetime import datetime
>>> datetime.fromtimestamp(1485714600).strftime("%A, %B %d, %Y %I:%M:%S")
'Sunday, January 29, 2017 08:30:00'

上記をまとめると、

from datetime import datetime
import time

origin = datetime.strptime("2021-oct-01 12:00", "%Y-%b-%d %H:%M")
origin_unix = time.mktime(origin.timetuple())
times = [datetime.fromtimestamp(origin_unix+t).strftime("%A, %B %d, %Y %I:%M:%S") for t in df['Time'].tolist()]
tf = pd.DataFrame(times,columns=["Time"])

これでTime(秒)が時刻に変換できた。

>>> tf.head()

	Time
0 	Friday, October 01, 2021 12:00:22
1 	Friday, October 01, 2021 12:00:44
2 	Friday, October 01, 2021 12:01:06
3 	Friday, October 01, 2021 12:01:28
4 	Friday, October 01, 2021 12:01:50

あとは元データと連結すればいい。

filtered = df.iloc[:, 1:] # Rows other than time
df2 = pd.concat([tf,filtered],axis=1)
df2.head()

Time 	Temperature 	Humidity 	CO2 	Soil Moisture
0 	Friday, October 01, 2021 12:00:22 	20.7 	79.0 	380.0 	80.0
1 	Friday, October 01, 2021 12:00:44 	20.7 	80.0 	380.0 	87.0
2 	Friday, October 01, 2021 12:01:06 	20.7 	80.0 	396.0 	88.0
3 	Friday, October 01, 2021 12:01:28 	20.6 	79.0 	399.0 	87.0
4 	Friday, October 01, 2021 12:01:50 	20.6 	79.0 	399.0 	87.0

時刻を含むPandasデータのプロットにはindexを指定する。
stackoverflow.com

df2.set_index('Time').plot()

f:id:seinzumtode:20220118204611p:plain