簡體   English   中英

pandas Sqlite 和重采樣錯誤僅對 DatetimeIndex 有效

[英]pandas Sqlite and resample error Only valid with DatetimeIndex

我正在嘗試使用 pandas 和 sqlite 將數據讀入數據幀。

如果我從 CSV 文件中讀取數據,我認為這段重新采樣到每小時平均值的代碼是有效的,但我不確定為什么從 Sqlite 中讀取數據? 抱歉,我對 db 知之甚少,非常感謝任何提示..

如果我運行下面的代碼,我可以打印第一個 df 但重采樣錯誤:

import pandas as pd
from sqlalchemy import create_engine
import sqlite3


con = 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         kW
0     2020-10-08 12:23:30.968967  68.129997
1     2020-10-08 12:25:39.375298  68.129997
2     2020-10-08 12:26:52.939991  68.129997
3     2020-10-08 12:27:57.839540  68.129997
4     2020-10-08 12:29:02.382524  68.129997
...                          ...        ...
1917  2020-10-09 10:14:35.113254  68.149994
1918  2020-10-09 10:15:08.840759  68.189995
1919  2020-10-09 10:15:41.873328  68.249992
1920  2020-10-09 10:16:14.953312  68.289993
1921  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'
>>> 

編輯

這似乎可以將日期時間索引從index轉換為日期DatetimeIndex index

df.index=pd.to_datetime(df.index)

請參閱此其他 SO POST。

您需要使用Datetimeindex而您忘記了inplace=True

嘗試這個:

df.set_index('Date', inplace=True)

而不是這個:

df.set_index('Date')

這應該解決它。

您可以在此處獲取有關Datatimeindex 的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM