не определяется ввод даты в функции в питон

Рейтинг: 0Ответов: 1Опубликовано: 12.02.2023
    import datetime
    def aiv(orders_in_hours, FactData,FactHourStart, FactHourFinish, DS, DeliveryType): 
    svod = orders_in_hours[(orders_in_hours['FactData'] >= FactData) & (orders_in_hours['FactHour'] >= FactHourStart) & (orders_in_hours['FactHour'] <= FactHourFinish) & (orders_in_hours['DS'] == DS) & (orders_in_hours['DeliveryType'] == DeliveryType)]
    group_order_svod = svod.groupby('FactHour')['Orders_D'].sum()
    group_item_svod = svod.groupby('FactHour')['Items_D'].sum()
    aiv_svod = pd.concat([group_order_svod, group_item_svod], sort = False, axis = 1)
    aiv_svod.reset_index(inplace=True)
    aiv_svod['AIV'] = round((aiv_svod['Items_D'] / aiv_svod['Orders_D'] ), 2)
    return aiv_svod

    print(aiv(orders_in_hours, '2023-01-21', 12, 23, 'FRESH_MAX_МСК_ПРАВОБЕРЕЖНЫЙ', 'Express'))

ошибка:

KeyError                                  Traceback (most recent call last)
/Library/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'FactData'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-53-5f0704eeb3a9> in <module>
----> 1 print(aiv(orders_in_hours, '2023-01-21', 12, 23, 'FRESH_MAX_МСК_ПРАВОБЕРЕЖНЫЙ', 'Express'))

<ipython-input-50-adb4a56dc20c> in aiv(orders_in_hours, FactData, FactHourStart, FactHourFinish, DS, DeliveryType)
      1 import datetime
      2 def aiv(orders_in_hours, FactData,FactHourStart, FactHourFinish, DS, DeliveryType):
----> 3     svod = orders_in_hours[(orders_in_hours['FactData'] >= FactData) & (orders_in_hours['FactHour'] >= FactHourStart) & (orders_in_hours['FactHour'] <= FactHourFinish) & (orders_in_hours['DS'] == DS) & (orders_in_hours['DeliveryType'] == DeliveryType)]
      4     group_order_svod = svod.groupby('FactHour')['Orders_D'].sum()
      5     group_item_svod = svod.groupby('FactHour')['Items_D'].sum()

/Library/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   3022             if self.columns.nlevels > 1:
   3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
   3025             if is_integer(indexer):
   3026                 indexer = [indexer]

/Library/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 'FactData'

Видимо ошибка с вводом даты в функцию.
Перепробовал все операции с датой, так и не получилось.
Хелп, как исправить?
В исходной таблице дата формата datatime 2023-01-21

Ответы

▲ 0
dt = datetime.date(2023, 1, 15).isoformat() 

поместил в функцию параметр dt и все заработало.