【问题标题】:Trouble resampling data in Pandas在 Pandas 中重新采样数据时遇到问题
【发布时间】:2016-05-10 03:27:46
【问题描述】:

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

我已成功将“时间”列设置为日期时间索引,但是当我尝试重新采样时,我不断收到“DataError: No numeric types to aggregate”

我也尝试过使用 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

【问题讨论】:

  • 使用df.dtypes,您可以看到数据框中每一列的数据类型。显然你没有任何数字列。
  • @ayhan df.dtypes 返回所有对象,索引除外。
  • @Alexander 我已经删除了所有不包含整数的列(风向等)。
  • 时间 TemperatureF DewpointF PressureIn \ 2016-01-01 00:04:00 2016-01-01 00:04:00 31.9 22.2 30.51 2016-01-01 00:10:00 2016-01- 01 00:10:00 32.2 22.5 30.52 2016-01-01 00:16:00 2016-01-01 00:16:00 32.5 23.1 30.51 2016-01-01 00:22:00 2016-01-01 00:22 :00 32.5 22.8 30.52
  • 好吧,至少现在我收到一条不同的错误消息...“TypeError: cannot astype a datetimelike from [datetime64[ns]] to [float64]” - 有没有办法排除日期列?

标签: python pandas resampling


【解决方案1】:

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

修复它:

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

【讨论】:

  • 哇。回想起来如此简单明了。感谢您发现问题并提供帮助!对于提供有用建议的其他人,对于通过提供不完整的问题描述而导致任何人误入歧途,我深表歉意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 2016-10-03
  • 2019-04-15
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
相关资源
最近更新 更多