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.
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.