2016-02-24 958 views
0

嗨我有以下代码,并且我试图将True分配给一个新列,其中实际日期等于'D'(创建日期)中的日期,并且任何其他值都为False。Python:为什么我会得到:“值的长度与索引长度不匹配”错误?

我很新的Python的,所以我想知道我做了什么错:

def GetData(): 
    myList = GetFileList(TodaysDate,5) 
    NewDataFrame = pd.DataFrame() 
    for x in myList: 
     #The date of the actuals data is the day BEFORE it was created 
     ActualDate = getDate(x) - timedelta(days=1) 

     myTempData = pd.read_csv(WSIWeatherDir + "\\" + x, parse_dates = [" date"], date_parser = DateTimeParser) 

     myTempData = myTempData.replace(-99,np.nan) 
     myTempData = myTempData.loc[myTempData['name'].isin(NL_WeatherStations)] 
     myTempData['D'] = myTempData[' date'].dt.date 

     #MyData = myTempData.sort([' date'], ascending=True) 
     #print MyData 

     #Select indices of the weather file where the column " date" is equal to the actual date we are looking for 
     MyActualIndex = myTempData['D'] == ActualDate 
     MyActualData = myTempData[MyActualIndex] 


     MyExpectedIndex = myTempData.index.difference(MyActualData.index) 
     MyExpectedData = myTempData.loc[MyExpectedIndex] 

     myTempData ['Actuals'] = [True] * len(MyActualData.index) 
     myTempData ['Actuals'] = [False] * len(MyExpectedData.index) 

     NewDataFrame = pd.concat([NewDataFrame,myTempData]) 
    return NewDataFrame 
print GetData() 

错误

runfile('C:/Users/aobenauff/Desktop/NLG_TAC_Calculation/TAC_2.py', wdir='C:/Users/aobenauff/Desktop/NLG_TAC_Calculation') 
Traceback (most recent call last): 

    File "<ipython-input-4-c9c151bca95a>", line 1, in <module> 
    runfile('C:/Users/aobenauff/Desktop/NLG_TAC_Calculation/TAC_2.py', wdir='C:/Users/aobenauff/Desktop/NLG_TAC_Calculation') 

    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile 
    execfile(filename, namespace) 

    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 

    File "C:/Users/aobenauff/Desktop/NLG_TAC_Calculation/TAC_2.py", line 117, in <module> 
    print GetData() 

    File "C:/Users/aobenauff/Desktop/NLG_TAC_Calculation/TAC_2.py", line 108, in GetData 
    myTempData ['Actuals'] = [True] * len(MyActualData.index) 

    File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2127, in __setitem__ 
    self._set_item(key, value) 

    File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2204, in _set_item 
    value = self._sanitize_column(key, value) 

    File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2362, in _sanitize_column 
    value = _sanitize_index(value, self.index, copy=False) 

    File "C:\Python27\lib\site-packages\pandas\core\series.py", line 2579, in _sanitize_index 
    raise ValueError('Length of values does not match length of ' 

ValueError: Length of values does not match length of index 
+0

请张贴完整的错误,包括堆栈跟踪,以帮助我们找出您的问题。 – deceze

回答

6

我最好的猜测依赖于这一部分:

myTempData ['Actuals'] = [True] * len(MyActualData.index) 
myTempData ['Actuals'] = [False] * len(MyExpectedData.index) 

首先说myTempData['Actuals']是一列大小为len(MyActualData.index)只包含值为。接下来,它将替换包含False值的另一列大小为len(MyExpectedData.index)(我预计会不同)的所有内容。

您可以先创建True值的列和,只有到那时,取代False的:

myTempData['Actuals'] = True 
myTempData.iloc[MyExpectedIndex] = False 
+0

是的,为我工作,谢谢 –

+0

太棒了!选择这个答案来帮助其他有相同问题的人;) –

相关问题