我觉得你可以先转换柱sec
to_timedelta
,set_index
和resample
通过2 seconds
(2S
):
df['sec'] = pd.to_timedelta(df.sec, unit='s')
df.set_index('sec', inplace=True)
print (df)
nanosec value
sec
00:16:41 1 0.2
00:16:41 2 0.2
00:16:41 3 0.2
00:16:42 1 0.1
00:16:42 2 0.2
00:16:42 3 0.1
00:16:43 1 0.2
00:16:43 2 0.2
00:16:43 3 0.1
00:16:44 1 0.2
00:16:44 2 0.2
00:16:44 3 0.2
00:16:44 4 0.1
print (df.value.resample('2S').mean())
sec
00:16:41 0.166667
00:16:43 0.171429
00:16:45 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S').std())
sec
00:16:41 0.051640
00:16:43 0.048795
00:16:45 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S').max())
sec
00:16:41 0.2
00:16:43 0.2
00:16:45 NaN
Freq: 2S, Name: value, dtype: float64
也许你需要在resample
改变base
:
print (df.value.resample('2S', base=1).mean())
sec
00:16:42 0.166667
00:16:44 0.171429
00:16:46 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S', base=1).std())
sec
00:16:42 0.051640
00:16:44 0.048795
00:16:46 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S', base=1).max())
sec
00:16:42 0.2
00:16:44 0.2
00:16:46 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S', base=2).mean())
sec
00:16:43 0.166667
00:16:45 0.171429
00:16:47 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S', base=2).std())
sec
00:16:43 0.051640
00:16:45 0.048795
00:16:47 NaN
Freq: 2S, Name: value, dtype: float64
print (df.value.resample('2S', base=2).max())
sec
00:16:43 0.2
00:16:45 0.2
00:16:47 NaN
Freq: 2S, Name: value, dtype: float64
我认为这会工作,但我得到一个警告尝试使用的.loc [ROW_INDEX,col_indexer] =值代替 DF1 [ 'header_stamp_secs'] = pd.to_timedelta(df1.header_stam p_secs,unit ='s')。这后面跟一个错误只对DatetimeIndex或PeriodIndex有效 – AMisra
有趣。你的熊猫版本是什么? – jezrael
我使用熊猫0.13.1 – AMisra