0

I'm trying to resample some data in 1 minute intervals and use the mean average to represent that minute of data, but I keep getting an error:

DataError: No numeric types to aggregate.

if I try min() ,max() or sum() it works fine but not with mean.

for example:
mdf = mdf.resample('1min').sum() will work but not
mdf = mdf.resample('1min').mean()

I've tried converting the dtypes using but I don't understand how to do this. mdf = pd.to_numeric(mdf) gives the following error,

TypeError: arg must be a list, tuple, 1-d array, or Series

The dtypes for my dataframe are all objects as shown below:

 - NO                 object
 - NO2                object
 - O3                 object
 - ambHumidity        object
 - ambPressure        object
 - ambTempC           object
 - humidity           object
 - particulatePM1     object
 - particulatePM10    object
 - particulatePM25    object
 - tempC              object

This is what the dataframe looks like This is what the dataframe looks like

Riyad
  • 1
  • 2
  • It'll be easier for folks to help if you include your data in the post as text rather than an image, so that people can copy the data and play with it. This post here gives a lot of help in how to make good, reproducible code/data: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – scotscotmcc Aug 19 '21 at 15:53
  • If your data is stored as strings, then some mathematical operations may not work if they don't "know" how to implicitly convert data types. You can use pandas [to_numeric](https://pandas.pydata.org/docs/reference/api/pandas.to_numeric.html) to convert your object columns to numeric data that can then be "mathed" – G. Anderson Aug 19 '21 at 15:56
  • Thanks for the advice both of you. @G.Anderson, you were right and now I managed to resolve the issue :) – Riyad Aug 19 '21 at 16:35
  • @scotscotmcc I tried recreating the dataframe but the dtypes aren't the same. my dataframe was made when I converted JSON file to a dataframe so the types didn't automatically change. I don't know how to recreate that unfortunately – Riyad Aug 19 '21 at 16:40

1 Answers1

0

You need to first convert dtypes in all columns of the dataframe to valid data type so that mathematical functions like mean can be used.

You can convert all the columns of a DataFrame via the apply() method:

df = df.apply(pd.to_numeric)

see converting types in pandas

Riyad
  • 1
  • 2
  • that didn't work, type error. TypeError: arg must be a list, tuple, 1-d array, or Series I tried pd.to_numeric(df) before and I got the same error. – Riyad Aug 19 '21 at 16:56
  • You're right, I forgot it only operates on a series at a time. Cheerfully withdrawn! – G. Anderson Aug 19 '21 at 17:20