I have a dataframe (df) which looks like this:
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):
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
:
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:
df['Temp'] = pd.to_numeric(df['Temp'], errors='coerce')
df2 = df.resample('H')['Temp'].agg(['mean','std'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.