gpt4 book ai didi

python - 在 Pandas 中重新采样数据时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:32 25 4
gpt4 key购买 nike

我正在尝试使用 Pandas 对天气数据进行重新采样。原始数据大约以 5 分钟为间隔。最后,我想导出单独的 excel 文件,其中包含以 5 分钟、15 分钟和 1 小时为间隔重新采样的数据。

我已成功将“时间”列设置为日期时间索引,但是当我尝试重新采样时,我不断收到“数据错误:没有要聚合的数字类型”

我还尝试使用 converters={'TemperatureF':int...etc 导入原始 excel 文件

#Open Excel File With Original Timestamps
xlsx = pd.ExcelFile('IDLWeaterData_OriginalTime.xlsx')
df = pd.read_excel(xlsx)
print ('File read successfully')

# Set 'Time' Column as dataframe index
df.set_index(pd.DatetimeIndex(pd.to_datetime(df.Time)), inplace=True)
df.drop(['Time'],axis=1)

#Resample to 5 minute intervals
clean5 = df.resample('5min').mean()

如果能深入了解导致此问题的原因,那就太好了!谢谢!

这是一个数据示例:

                    TemperatureF    DewpointF   PressureIn  Humidity    HourlyPrecipIn  dailyrainin SolarRadiationWatts/m^2
2016-01-01 00:04:00 31.9 22.2 30.51 67 0.00 0.00 0
2016-01-01 00:10:00 32.2 22.5 30.52 67 0.00 0.00 0
2016-01-01 00:16:00 32.5 23.1 30.51 68 0.00 0.00 0

最佳答案

这就是您的数据所发生的情况。

enter image description here

修复它:

>>> df[df.Time.notnull()].set_index('Time').astype(float).resample('5min')
TemperatureF DewpointF PressureIn Humidity HourlyPrecipIn

dailyrainin SolarRadiationWatts/m^2
Time
2016-01-01 00:00:00 31.9 22.20 30.51 67 0 0 0
2016-01-01 00:05:00 NaN NaN NaN NaN NaN NaN NaN
2016-01-01 00:10:00 32.2 22.50 30.52 67 0 0 0
2016-01-01 00:15:00 32.5 23.10 30.51 68 0 0 0
2016-01-01 00:20:00 32.5 22.80 30.52 67 0 0 0
...

关于python - 在 Pandas 中重新采样数据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com