Python 类型错误:无法连接非 NDFrame 对象,当时间序列混杂时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32149739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:06:15  来源:igfitidea点击:

TypeError: cannot concatenate a non-NDFrame object, when time series mungling

pythonpandas

提问by Hello lad

Have a time series ts (dataframe.to_dict())

有一个时间序列 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}}

and a customized aggregation function (example)

和自定义聚合函数(示例)

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

want to aggregate time series with customized function by 10 min, so

想要将时间序列与自定义函数聚合 10 分钟,所以

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

then instead of correct result, it gives me error

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

TypeError: cannot concatenate a non-NDFrame object

What does this error say ? How could I write the code correctly ? thx a lot

这个错误说明了什么?我怎么能正确编写代码?多谢

采纳答案by CT Zhu

I think you want to agg(aggregate), not apply, as for each of your group, you want 1 returning value:

我认为您想要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

回答by Joseph Farah

pd.DataFrame()might be what you are looking for. It allows you to parse two dimensional dictionaries (or anything, really with a tabular structure). Here is the documentation for the function: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html#pandas.DataFrame

pd.DataFrame()可能是你正在寻找的。它允许您解析二维字典(或任何东西,真正具有表格结构)。这是该函数的文档:http: //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html#pandas.DataFrame