请帮助解决这个问题

时间:2021-03-18 04:48:35

标签: python python-3.x pandas

#Merging dat with time mapping file so that we hvae all intervals data
today = str(dt.date.today())
new_date =  today+' 00:00:00'

#####Merging with forecast file##########################################################################################
def merging_time_forecast(Lob, Lob1,sch,oncall):
    Lob = pd.merge(time_mapping,Lob, how = 'inner', on = 'interval')
    Lob1.reset_index(drop=True,inplace=True)
    #Lob1.to_excel("c:\\Flipkart\\Flipkart\\Input\\Mappings\\test.xlsx", index = False) # this delibratly done so that we can map forecast
    #foreast_test = pd.read_excel('C:\\Flipkart\\Flipkart\\Input\\Mappings\\test.xlsx')# this delibratly done so that we can map forecast
    sch.columns = (sch.columns).astype(str)
    oncall.columns = (oncall.columns).astype(str)
    Lob['Forecast'] = Lob1[today]
    Lob['Schedule'] = sch[new_date]
    Lob['Sch_post_shrinkage'] = oncall[new_date].round()
   
    Lob.fillna(0, inplace = True)
    #os.remove('C:\\Flipkart\\Flipkart\\Input\\Mappings\\test.xlsx')
    return Lob
call_stats_cmb_ss=merging_time_forecast(call_stats_cmb_ss,forecast_cmb_ss,cmb_ss_schedule,cmb_ss_oncall)
call_stats_cmb_ss_Baroda=merging_time_forecast(call_stats_cmb_ss_Baroda,forecast_cmb_ss_Baroda,cmb_ss_schedule_Baroda,cmb_ss_oncall_Baroda)
call_stats_rmt=merging_time_forecast(call_stats_rmt,forecast_rmt,rmt_schedule,rmt_oncall)

call_stats_wm=pd.merge(time_mapping,call_stats_wm, how = 'inner', on = 'interval')
call_stats_wm.fillna(0, inplace = True)

Traceback (most recent call last):
  File "C:\Users\COMP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '2021-03-18 00:00:00'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Flipkart\Flipkart\Exe-file\Flipkart AI-V1.3.py", line 169, in <module>
    call_stats_rmt=merging_time_forecast(call_stats_rmt,forecast_rmt,rmt_schedule,rmt_oncall)
  File "c:\Flipkart\Flipkart\Exe-file\Flipkart AI-V1.3.py", line 161, in merging_time_forecast
    Lob['Schedule'] = sch[new_date]
  File "C:\Users\COMP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\COMP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '2021-03-18 00:00:00'

1 个答案:

答案 0 :(得分:1)

问题出在这一行:

Lob['Schedule'] = sch[new_date]

如果您尝试通过索引(日期)获取

使用sch.loc[<index>][<column>]

有关详细信息,请参阅this

由于您尚未发布数据。我只能说调试这个 sch[new_date] 部分