此代码在我的另一台使用 NumPy 1.6 的计算机上运行:
import pandas as pd
from pandas import DataFrame
import numpy as np
datapath='C:/Users/Alex/Desktop/samoa/WATERSHED_ANALYSIS/FAGAALU/MasterDataFiles/FP-Master.csv'#):
col_names = ['Date', 'Time', 'TempOut', 'HiTemp', 'LowTemp', 'OutHum', 'DewPt', 'WindSpeed', 'WindDir', 'WindRun', 'HiSpeed', 'HiDir', 'WindChill', 'HeatIndex', 'THWIndex', 'Bar', 'Rain', 'RainRate', 'HeatD-D', 'CoolD-D', 'InTemp', 'InHum', 'InDew', 'InHeat', 'InEMC', 'InAirDensity', 'WindSamp', 'WindTx', 'ISSRecept', 'Arc.Int.']
Wx= pd.read_csv(datapath,skiprows=1,header=0,names=col_names,parse_dates=[['Date','Time']],index_col=['Date_Time'],na_values=['---'])
Wx.index = Wx.index.astype('datetime64')
Wx = Wx.resample('15Min',fill_method='pad',limit=2) ## fill the 30min intervals to 15minute
该列'Date_Time'
是 csv 文件列的组合,'Date'
并且'Time'
已格式化"%m/%d/%Y %I:%M %p"
在带有 NumPy 1.7 的新计算机上,我收到此错误:
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pandas\core\index.py", line 198, in astype
return Index(self.values.astype(dtype), name=self.name,
ValueError: Cannot create a NumPy datetime other than NaT with generic units
我尝试使用Wx.index = pd.to_datetime(Wx.index)
,但无法将索引转换为DatetimeIndex
.
我也尝试过使用
Wx.index = Wx['Date_Time'].convert_objects(convert_dates='coerce')
它将索引转换为pandas.tseries.index.DatetimeIndex
,但随后
Wx.resample('15Min',,fill_method='pad',limit=2)
给出这个错误:
File "tslib.pyx", line 1978, in pandas.tslib.normalize_date (pandas\tslib.c:30569)
ValueError: month must be in 1..12
有谁知道为什么这不起作用?我试过使用.asfreq('15Min')
then .fillna('pad')
,但它很笨拙,需要对其他模块进行大量重新编码。