简体   繁体   中英

Resample Pandas dataframe not valid

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

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?

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()

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.

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