[英]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. 我尝试使用pandas导入.csv 30分钟时间序列文件,将其重新采样到几小时,但重采样功能无法识别日期时间格式。
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'. 下面是我目前使用的代码,日期和时间是.csv文件中的分隔列,并由脚本合并为“datetime”。 The .csv consists of 8 columns and 5131 rows.
.csv由8列和5131行组成。
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. 首先将数据帧的索引设置为
datetime
列,将其转换为日期时间索引,它应该可以工作。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.