Pandas KeyError on last 'groupby' value

Viewed 27

I have a code where I have concatenated multiple weather files into one .csv, where I group by two columns and get the sum for the remaining columns.

if Meteo == 'Evaporation':
                    ##find all files with 1Evap
                    meteo_files = glob.glob(working_folder + "1Evaporation*.csv")
                    ##concat together
                    df = pd.concat(map(pd.read_csv, meteo_files), ignore_index = True)
                    ##assigning folder names
                    final_csv_1 = working_folder + "2%s.csv" %(Meteo)
                    final_csv = working_folder + "%s.csv" %(Meteo)
                    #write concat df to csv
                    df.to_csv(final_csv_1, index=False,header=False)
                    ##read in concat csv
                    df = pd.read_csv(final_csv_1)
                    ##group by two columns, sum the rest
                    df.groupby([yyyymmdd, HHMMSS])[PET, PE, PETree, PESnow, PETreeSnow].sum()
                    ##write final csv
                    df.to_csv(final_csv, header=False, index=False)

where I get this error (20211230 is the final yyyymmdd value):

Traceback (most recent call last):
  File "D:\Downloads\utmtest.py", line 235, in <module>
    df.groupby([yyyymmdd, HHMMSS], as_index = False)[Tair_F, Tdew_F, NetRad, WndSpd, Precip, SnowDepth].sum()
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\frame.py", line 7631, in groupby
    return DataFrameGroupBy(
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\groupby\groupby.py", line 889, in __init__
    grouper, exclusions, obj = get_grouper(
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\groupby\grouper.py", line 862, in get_grouper
    raise KeyError(gpr)
KeyError: '20211230'

If I change the length of the df, say to 20211130 being the end, I get the same error but with KeyError: '20211130' I've seen a lot of keyerror questions, but it has nothing to do with my column name, rather the values. What am I missing here? Thank you!

0 Answers
Related