4

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!

ValientProcess
  • 1,699
  • 5
  • 27
  • 43

1 Answers1

6

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 strings) use to_numeric for replace them to NaNs:

df['Temp'] = pd.to_numeric(df['Temp'], errors='coerce')
df2 = df.resample('H')['Temp'].agg(['mean','std'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • First of all thanks! I do need to convert to string, but when i'm trying your above to_numeric i'm getting the following: D:\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app – ValientProcess Jul 22 '17 at 11:45
  • Obviously it means some problem in code above. Maybe need `copy` like [this](https://stackoverflow.com/a/45170475/2901002). – jezrael Jul 22 '17 at 11:48
  • Or maybe some `[]` problem, I think you can check [docs](http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy) – jezrael Jul 22 '17 at 11:49