简体   繁体   English

重采样Pandas数据帧无效

[英]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分钟时间序列文件,将其重新采样到几小时,但重采样功能无法识别日期时间格式。

  1. Import works correct with a script found on stackoverflow. 使用stackoverflow上找到的脚本导入工作正确。
  2. When I open the Dataframe and double-click on a date+time it mentions that I can't edit a Timestamp. 当我打开Dataframe并双击日期+时间时,它提到我无法编辑时间戳。
  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'' 当我尝试重新采样DataFrame时,它给出了TypeError:'仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到了'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'. 下面是我目前使用的代码,日期和时间是.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.

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