简体   繁体   English

DataFrame重采样(熊猫)中无结果

[英]No Results in DataFrame Resample (pandas)

I have a dataframe (df) which looks like this: 我有一个数据帧(df),如下所示:

      Time          Temp
2017-01-01 00:30:00 11.1
2017-01-01 01:00:00 10.8
2017-01-01 01:30:00 10.8
2017-01-01 02:00:00 10.8
2017-01-01 02:30:00 11.1
.....             ....

I'm trying to get the hourly averages of the Temp data, I used to do it with the following code (Time is the index): 我试图获取Temp数据的每小时平均值,我曾经用以下代码(时间是索引)来做到这一点:

df2 = df.resample('H').agg(['mean','std'])

But now I'm getting the following error: 但是现在我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-b43bf44dcae3> in <module>()
----> 1 df9 = dfroof4.resample('H').agg(['mean','std'])

D:\Anaconda3\lib\site-packages\pandas\core\resample.py in aggregate(self, arg, *args, **kwargs)
    314 
    315         self._set_binner()
--> 316         result, how = self._aggregate(arg, *args, **kwargs)
    317         if result is None:
    318             result = self._groupby_and_aggregate(arg,

D:\Anaconda3\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
    632             return self._aggregate_multiple_funcs(arg,
    633                                                   _level=_level,
--> 634                                                   _axis=_axis), None
    635         else:
    636             result = None

D:\Anaconda3\lib\site-packages\pandas\core\base.py in _aggregate_multiple_funcs(self, arg, _level, _axis)
    689         # if we are empty
    690         if not len(results):
--> 691             raise ValueError("no results")
    692 
    693         try:

ValueError: no results

Any ideas? 有任何想法吗?

EDIT : 编辑

the output of 输出

print(df.dtypes)

is: 是:

Temp    object
dtype: object

Thanks! 谢谢!

You need cast to float first by astype : 你需要转换为float首先astype

df['Temp'] = df['Temp'].astype(float)
df2 = df.resample('H')['Temp'].agg(['mean','std'])

If some bad data (like string s) use to_numeric for replace them to NaN s: 如果某些不良数据(例如string s)使用to_numeric替换为NaN

df['Temp'] = pd.to_numeric(df['Temp'], errors='coerce')
df2 = df.resample('H')['Temp'].agg(['mean','std'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM