简体   繁体   English

pandas Sqlite 和重采样错误仅对 DatetimeIndex 有效

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

I am experimenting with pandas and sqlite to read data into a dataframe.我正在尝试使用 pandas 和 sqlite 将数据读入数据帧。

I think this code to resample to hourly averages worked if I were reading from a CSV file, but I am not sure why from Sqlite?如果我从 CSV 文件中读取数据,我认为这段重新采样到每小时平均值的代码是有效的,但我不确定为什么从 Sqlite 中读取数据? Sorry I know very little about db any tips greatly appreciated..抱歉,我对 db 知之甚少,非常感谢任何提示..

If I run the code below I can print the first df but the resample errors:如果我运行下面的代码,我可以打印第一个 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)

Output:输出:

>>> 
=== 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'
>>> 

EDIT编辑

This appeared to work converting the datetime index from index to DatetimeIndex这似乎可以将日期时间索引从index转换为日期DatetimeIndex index

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

See this other SO POST. 请参阅此其他 SO POST。

You need to use a Datetimeindex and your forgot the inplace=True您需要使用Datetimeindex而您忘记了inplace=True

try this:尝试这个:

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

instead of this:而不是这个:

df.set_index('Date')

this should solve it.这应该解决它。

You can get more information about Datatimeindex here您可以在此处获取有关Datatimeindex 的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas dataframe.resample TypeError '仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例 - Pandas dataframe.resample TypeError 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' Pandas 重采样错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效 - Pandas Resampling error: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex 重新采样到DatetimeIndex会引发错误 - Resample to DatetimeIndex raises an error Pandas resample函数不能在DateTimeIndex上工作 - Pandas resample function not working on DateTimeIndex 基于 DatetimeIndex 变量重新采样 Pandas Dataframe - Resample Pandas Dataframe based on a DatetimeIndex variable 尝试使用 DatetimeIndex 重新采样 Pandas dataframe 时出现 AttributeError - AttributeError while trying to resample a Pandas dataframe with DatetimeIndex Pandas:重新采样 dataframe 以匹配不同 dataframe 的 DatetimeIndex - Pandas: resample a dataframe to match a DatetimeIndex of a different dataframe Pandas DateTimeIndex 多个 groupby 或 resample 聚合 - Pandas DateTimeIndex multiple groupby or resample aggregation pandas 不能按日期分组,仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但 - pandas cannot group by date, Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but 重采样Pandas数据帧无效 - Resample Pandas dataframe not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM