Select to view content in your preferred language

Query function throwing key error for field that is not called

537
0
08-10-2023 03:28 PM
ccowin_odfw
Occasional Contributor

So I'm not understanding an error I'm getting.

I've had the following where statement work just fine in my testing: 'Season = 2022 and Day_of_Season = 179'

However, when I took off the second condition it is throwing a KeyError for a field I'm not even querying. It really makes no sense. It is the only date field, but why would it work with one where statement but not the other?

It does work without the out_field parameter with both where statements, but it isn't included so why is it even doing anything with Hunt_Date?

That line:

 

df_db = self.db.query(where='Season = 2022', out_fields=['Mallard', 'Green_winged_Teal', 'American_Wigeon', 'Northern_Pintail', 'Northern_Shoveler', 'Gadwall', 'Wood_Duck', 'Cinnamon_Blue_winged_Teal', 'Eurasian_Wigeon', 'Ring_necked_Duck', 'Canvasback', 'Bufflehead', 'Scaup_Species', 'Goldeneye_Species', 'Ruddy_Duck', 'Redhead', 'Hooded_Merganser', 'Common_Merganser', 'Scoter_Species', 'Long_tailed_Duck', 'Other_Duck_Species', 'Lesser_Snow_Goose', 'Ring_necked_Pheasant', 'California_Quail', 'Dove', 'Coot', 'Snipe', 'Hunt_Unit', 'Major_Unit', 'Day_of_Season'], as_df=True)

 

The error:

 

Traceback (most recent call last):
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\pandas\core\indexes\base.py", line 3629, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Hunt_Date'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\arcgis\features\layer.py", line 4286, in query
    df[fld] / 1000, infer_datetime_format=True, unit="s"
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc
    raise KeyError(key) from err
KeyError: 'Hunt_Date'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\pandas\core\indexes\base.py", line 3629, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Hunt_Date'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\cowinch\AppData\Local\ESRI\conda\envs\arcgispro-py3-cowin\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc
    raise KeyError(key) from err
KeyError: 'Hunt_Date'

 

0 Replies