我正在嘗試使用 pandas 和 sqlite 將數(shù)據(jù)讀入數(shù)據(jù)幀。我認(rèn)為如果我從 CSV 文件中讀取數(shù)據(jù),這段重新采樣為每小時(shí)平均值的代碼是有效的,但我不確定為什么是從 Sqlite 讀取?抱歉,我對(duì)數(shù)據(jù)庫(kù)知之甚少,非常感謝任何提示。如果我運(yùn)行下面的代碼,我可以打印第一個(gè) df 但重新采樣錯(cuò)誤:import pandas as pdfrom sqlalchemy import create_engineimport sqlite3con = sqlite3.connect('./save_data.db')df = pd.read_sql("SELECT * from all_data", con, index_col='Date', parse_dates=True)df.set_index('Date')print(df)hourly_avg['kW'] = df['kW'].resample('H').mean()print('hourly_avg.kW', hourly_avg.kW)輸出:>>> === RESTART: C:\Users\Desktop\tester\Test.py === Date kW0 2020-10-08 12:23:30.968967 68.1299971 2020-10-08 12:25:39.375298 68.1299972 2020-10-08 12:26:52.939991 68.1299973 2020-10-08 12:27:57.839540 68.1299974 2020-10-08 12:29:02.382524 68.129997... ... ...1917 2020-10-09 10:14:35.113254 68.1499941918 2020-10-09 10:15:08.840759 68.1899951919 2020-10-09 10:15:41.873328 68.2499921920 2020-10-09 10:16:14.953312 68.2899931921 2020-10-09 10:16:48.043465 68.289993[1922 rows x 2 columns]Traceback (most recent call last): File "C:\Users\Desktop\tester\Test.py", line 11, in <module> hourly_avg['kW'] = df['kW'].resample('H').mean() File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\generic.py", line 8087, in resample offset=offset, File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\resample.py", line 1269, in get_resampler return tg._get_resampler(obj, kind=kind) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\resample.py", line 1435, in _get_resampler "Only valid with DatetimeIndex, "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'>>>
1 回答

神不在的星期二
TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
你需要使用 aDatetimeindex
但你忘記了inplace=True
嘗試這個(gè):
df.set_index('Date',?inplace=True)
而不是這個(gè):
df.set_index('Date')
這應(yīng)該可以解決它。
添加回答
舉報(bào)
0/150
提交
取消