Resample Pandas dataframe not valid

Asked by At

I try to import a .csv 30-minutes-timeseries file with pandas to resample it to hours but the resample function doesn't recognise the datetime format.

  1. Import works correct with a script found on stackoverflow.
  2. When I open the Dataframe and double-click on a date+time it mentions that I can't edit a Timestamp.
  3. When I try te resample the DataFrame it gives the TypeError: 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex''

Below is the code that I'm currently using, the date and time are separated columns in the .csv file and merged by the script into 'datetime'. The .csv consists of 8 columns and 5131 rows.

def dateparse(d,t):
    dt = d + " " + t
    return pd.datetime.strptime(dt, '%d/%m/%Y %H:%M:%S')
df = pd.read_csv(infile, parse_dates={'datetime': ['date', 'time']}, date_parser=dateparse)

df.resample('H').mean()

Anyone familiair with this problem?

1 Answers

0
Tabbakhh On Best Solutions

Set the index of the dataframe to the datetime column first, convert it to a datetime index, and it should work.

def dateparse(d,t):
    dt = d + " " + t
    return pd.datetime.strptime(dt, '%d/%m/%Y %H:%M:%S')
df = pd.read_csv(infile, parse_dates={'datetime': ['date', 'time']}, date_parser=dateparse)

df = df.set_index('datetime')
df.index = pd.to_datetime(df.index)
df.resample('H').mean()