Python 使用Dask中的read_sql_表将时间戳指定为索引
在SQLite中,我有一个带有索引列的表Python 使用Dask中的read_sql_表将时间戳指定为索引,python,sqlite,dask,Python,Sqlite,Dask,在SQLite中,我有一个带有索引列的表data,它是由time.time()在录制时生成的时间戳 我想将此表中的数据加载到Dask数据帧。为此,我使用: import dask.dataframe as dd data = dd.read_sql_table('data', 'sqlite:///'+DB_PATH, index_col='time', parse_dates={"time": {"unit":"s"}}) 当我想对
data
,它是由time.time()在录制时生成的时间戳
我想将此表中的数据加载到Dask数据帧。为此,我使用:
import dask.dataframe as dd
data = dd.read_sql_table('data', 'sqlite:///'+DB_PATH, index_col='time', parse_dates={"time": {"unit":"s"}})
当我想对数据data.resample('15S').mean()应用滚动平均值时,我得到:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Float64Index'
因此,如果我通过data.index
检查索引,它会给出这样的结果,这表明它的数据类型正确,parse_dates
确实有效,对吗
Dask Index Structure:
npartitions=1
1.619876e+09 datetime64[ns]
1.620067e+09 ...
Name: time, dtype: datetime64[ns]
Dask Name: from-delayed, 3 tasks
最后,我尝试在加载后定义索引datetime:
data = dd.read_sql_table('data', 'sqlite:///'+DB_PATH, index_col='time')
data['time__1'] = pd.to_datetime(np.array(data['time__1']), unit='s') # By the way, I don't know why Dask creates a 'time__1' column...
data = data.set_index('time__1', sorted=True)
但后来我得到了这个信息
ValueError: Length of values does not match length of index
我想出了一个解决办法。它似乎不是最有效的,因为它没有直接从read\u sql\u table()
中利用date\u parse,并且依赖于Dask出于某种原因生成的列time\u 1
data = dd.read_sql_table('data', 'sqlite:///'+DB_PATH, index_col='time')
data = data.set_index(data['time__1'].map_partitions(pd.to_datetime, unit='s'))
data = data.drop('time__1', axis=1)
如果你知道一个更好的解决方案