有一个时间序列ts(dataframe.to_dict())

{'latitude': {Timestamp('2014-10-20 15:21:56.571000'): 48.145553900000003,
  Timestamp('2014-10-20 15:24:00.789000'): 48.145584300000003,
  Timestamp('2014-10-20 15:26:00.911000'): 48.145497599999999,
  Timestamp('2014-10-20 15:33:57.764000'): 48.145548699999999,
  Timestamp('2014-10-20 15:36:45.760000'): 48.145454999999998},
  'longitude': {Timestamp('2014-10-20 15:21:56.571000'): 11.578263,
  Timestamp('2014-10-20 15:24:00.789000'): 11.5783685,
  Timestamp('2014-10-20 15:26:00.911000'): 11.578193499999999,
  Timestamp('2014-10-20 15:33:57.764000'): 11.5782843,
  Timestamp('2014-10-20 15:36:45.760000'): 11.5783164},
  'speed': {Timestamp('2014-10-20 15:21:56.571000'): 0.0,
  Timestamp('2014-10-20 15:24:00.789000'): 0.0,
  Timestamp('2014-10-20 15:26:00.911000'): 0.0,
  Timestamp('2014-10-20 15:33:57.764000'): 0.0,
  Timestamp('2014-10-20 15:36:45.760000'): 0.0}}


和定制的聚合功能(示例)

def my_func(group):
    first_latitude = group['latitude'].sort_index().head(1).values[0]
    last_longitude = group['longitude'].sort_index().tail(1).values[0]
    return first_latitude - last_longitude


想要将具有自定义功能的时间序列汇总到10分钟,所以

ts.groupby(pd.TimeGrouper(freq='10Min')).apply(my_func)


然后不是正确的结果,而是给我错误

TypeError: cannot concatenate a non-NDFrame object


这个错误说什么?如何正确编写代码?多谢

最佳答案

我认为您想要agg(汇总),而不是apply,因为对于您的每个组,您都想要1个返回值:

In [185]:

print ts.groupby(pd.TimeGrouper(freq='10Min')).agg(my_func)
                      latitude  longitude      speed
2014-10-20 15:20:00  36.567360  36.567360  36.567360
2014-10-20 15:30:00  36.567232  36.567232  36.567232

关于python - TypeError:时间序列杂乱无章时,无法连接非NDFrame对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32149739/

10-12 21:48