Python 如何使用时间戳对数据帧进行上采样

Python 如何使用时间戳对数据帧进行上采样,python,pandas,resampling,Python,Pandas,Resampling,我有这样一个数据帧(不关心NaN值): 我想每20毫秒增加一次采样 我所做的是: df = df.set_index('TIMESTAMP') df = df.resample('20ms').ffill() 但我得到了一个错误: Traceback (most recent call last): sens_encoded = sens_encoded.resample('20ms').ffill() TypeError: Only valid with DatetimeIndex, Ti

我有这样一个数据帧(不关心NaN值):

我想每20毫秒增加一次采样

我所做的是:

df = df.set_index('TIMESTAMP')
df = df.resample('20ms').ffill()
但我得到了一个错误:

Traceback (most recent call last):
sens_encoded = sens_encoded.resample('20ms').ffill()
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
Traceback (most recent call last):
df.index = pd.to_datetime(df.index)
TypeError: <class 'tuple'> is not convertible to datetime
因此我尝试将时间戳转换为DateTime,它应该已经:

df = df.set_index('TIMESTAMP')
df.index = pd.to_datetime(df.index)   //Added this
df = df.resample('20ms').ffill()
但我得到了一个错误:

Traceback (most recent call last):
sens_encoded = sens_encoded.resample('20ms').ffill()
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
Traceback (most recent call last):
df.index = pd.to_datetime(df.index)
TypeError: <class 'tuple'> is not convertible to datetime

df=pd.DataFrame(columns=[columns\u names])

这应该可以做到:

df.set_index(pd.to_datetime(df.TIMESTAMP), inplace=True)
df = df.resample('20ms').ffill()

首先将
多索引的第一级设置为列,以删除断开的一级
多索引

为将不可解析的值转换为
NaT
添加参数
errors='concurve'
,如有必要,也可以先转换列,然后创建
DatetimeIndex
和最后一次
upsample

df.columns = df.columns.get_level_values(0)

df['TIMESTAMP'] = pd.to_datetime(df['TIMESTAMP'], errors='coerce')
df = df.set_index('TIMESTAMP').resample('20ms').ffill()
或:


如何工作。它给出了
'tuple'对象没有属性'lower'
@FrancescoPegoraro-如何工作
df['TIMESTAMP']=df['TIMESTAMP'].apply(pd.TIMESTAMP)
?第二个给出:
值错误:NaTType不支持dst
第三个给出:
类型错误:(“'DataFrame'对象不可调用”,“发生在索引(TIMESTAMP,)”)
请检查有问题的编辑,可能会有所帮助!不。它使“tuple”对象没有属性“lower”