CSVファイル内の時系列データを1秒区切りで平均化するコードを書いているのですがエラーの解消方法を知りたいです


**前提**  
各列にTIME(データ取得日時)、測定値が格納されたCSVファイルを読み込んで、1秒毎に平均化してCSVファイルに出力したいです。  
コードを書いてみたのですがエラーが出てしまい対処方法が分からないので教えて頂けるとありがたいです。

下記は使用するCSVデータで名称は「test.csv」です。

[code]

    [LOGGING],RD81DL96_1,2,3,4
    DATETIME[YYYY/MM/DD hh:mm:ss.s],INDEX,BIT[1;0],BIT[1;0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],SHORT[DEC.0],TRIGGER[*;-]
    TIME
    2022/10/20 10:46:13.8,1987,1,0,0,0,0,29,13,22,12,0,0,0,0,0,0,0,0,0,7,4,2,1,4,4,2,77,5,1,12,4,42,4,0,0,3,5,11,6,6,1,8,89,3,5,7,11,34,8,0,0,253,305,340,378,380,384,427,309,298,301,291,295,283,292,293,298,407,574,418,294,203,122,161,0,0,0,0,0,0,0,0,0,236,453,3,332,246,613,772,854,817,922,945,853,957,685,344,253,18,239,468,468,490,467,46,644,0,0,0,0,0,0,0,0,103,29,113,50,88,157,108,103,63,69,96,88,68,72,104,104,92,50,85,80,55,96,48,0,0,0,0,0,0,0,0,0,63,97,196,78,112,55,74,123,128,90,96,78,222,185,76,54,90,39,51,36,57,52,49,39,192,116,18,24,62,8,25,73,76,131,89,122,116,118,28,9,12,77,58,50,4,124,79,129,
    2022/10/20 10:46:13.9,1988,1,0,0,0,0,7,13,22,12,0,0,0,0,0,0,0,0,0,6,5,1,1,3,3,1,408,4,3,12,4,43,3,0,0,3,5,11,8,6,2,7,46,1,4,5,11,34,7,0,0,255,307,341,376,378,384,443,310,310,294,299,294,279,283,289,285,414,553,558,300,235,101,134,0,0,0,0,0,0,0,0,0,272,467,21,332,319,536,755,845,788,804,868,900,908,760,333,257,6,240,426,429,500,467,47,666,0,0,0,0,0,0,0,0,106,38,114,52,91,152,113,111,64,56,108,82,30,87,94,74,57,61,97,75,61,85,58,0,0,0,0,0,0,0,0,0,72,62,212,90,81,47,69,100,88,97,89,75,188,155,51,20,98,52,56,33,44,53,52,55,198,119,96,26,59,16,26,19,123,143,110,114,109,149,90,9,10,70,56,0,165,111,109,133,
    2022/10/20 10:46:14.0,1989,1,0,0,0,0,3,12,22,13,0,0,0,0,0,0,0,0,0,6,5,1,2,2,4,1,5,4,1,11,4,43,3,0,0,2,4,12,7,7,2,7,0,2,4,5,11,33,6,0,0,253,305,338,372,376,384,440,339,313,305,299,291,281,300,287,281,363,465,615,337,306,137,133,0,0,0,0,0,0,0,0,0,289,398,44,344,310,398,745,835,768,853,916,879,900,802,348,256,8,208,409,448,496,465,42,585,0,0,0,0,0,0,0,0,89,50,127,35,118,177,138,101,73,71,108,85,62,47,80,127,55,56,95,153,61,198,43,0,0,0,0,0,0,0,0,0,34,82,160,99,40,55,60,80,60,118,104,82,119,128,42,102,78,47,67,32,31,45,45,49,189,124,101,23,60,17,23,12,157,124,104,123,119,140,132,1,9,71,57,9,209,131,91,128,
    2022/10/20 10:46:14.1,1990,1,0,0,0,0,4,12,22,12,0,0,0,0,0,0,0,0,0,6,6,1,1,3,4,1,7,4,1,13,4,43,3,0,0,1,5,11,7,7,1,7,3,1,4,5,11,35,7,0,0,253,304,335,367,374,384,437,372,313,306,307,298,283,304,281,279,319,370,616,426,363,246,93,0,0,0,0,0,0,0,0,0,281,438,31,341,322,262,615,824,737,840,918,925,908,816,369,253,94,127,399,408,536,486,48,452,0,0,0,0,0,0,0,0,98,51,110,58,104,162,114,89,74,80,94,82,36,77,87,119,1,66,77,90,56,130,66,0,0,0,0,0,0,0,0,0,49,94,220,111,63,40,134,101,73,78,129,68,115,131,49,88,99,45,59,34,40,50,59,43,192,128,88,98,53,17,22,69,89,129,122,115,119,133,123,42,11,63,60,71,230,99,114,136,
    2022/10/20 10:46:14.2,1991,1,0,0,0,0,1,13,22,11,0,0,0,0,0,0,0,0,0,7,5,2,1,3,3,1,7,596,2,10,3,42,2,0,0,3,6,13,7,8,2,8,4,130,4,6,11,32,8,0,0,252,304,334,366,372,383,432,375,333,312,302,289,290,311,286,280,301,358,519,434,298,346,152,0,0,0,0,0,0,0,0,0,277,431,44,368,314,243,481,817,711,877,965,913,905,827,424,282,90,120,375,399,518,524,55,426,0,0,0,0,0,0,0,0,98,34,105,50,80,163,104,110,76,78,76,82,59,65,76,106,39,55,81,121,55,84,51,0,0,0,0,0,0,0,0,0,80,74,212,82,83,60,90,33,92,88,135,59,85,113,42,100,92,58,65,36,47,46,56,40,209,125,101,116,51,20,23,26,58,93,129,127,118,138,124,113,12,59,58,79,120,84,103,137,
    
[/code]

**実現したいこと**  
・上記CSVデータを1秒刻みで平均化したい  
2022/10/20 10:46:13台は46:13.8から46:13.9の2行のデータで平均化  
2022/10/20 10:46:14台は46:14.0から46:14.2の3行のデータで平均化  
その1秒の中で持っているデータの行数が異なっていても、持っている個数に応じて平均化したい  
実際に使用するデータは上記のようなデータが無数に格納されるので、今回の46:13台、46:14台だけが持っている個数で平均化できれば良いわけでは無い

・平均化されたデータを再度CSVファイルに出力したい

**ソースコード**

[code]

    import pandas as pd
    
    df =pd.read_csv("test.csv",encoding="cp932",sep=',',index_col=0,parse_dates=True,skiprows=2)
    
    df['datetime'] = pd.to_datetime(df['TIME']) # 日時情報が入った列をdatetime型へ変換
    
    df.set_index('datetime',inplace=True) #indexにdatetime列を指定し、置き換え
    
    df_resample = df.resample('S').sum()
    
    df_resample.to_csv('output.comcsv')
    
[/code]

**発生している問題・エラーメッセージ**  
raise KeyError(key) from err  
KeyError: 'TIME'




csvの`TIME`列がインデックスとして指定されているので、`df['TIME']`の形式でカラムとして取得できません。  
`df['datetime'] = pd.to_datetime(df['TIME'])`を`df['datetime'] =
pd.to_datetime(df.index)`と書き換えることで正しく動作します。

