Python: Attribute lookup read_csvfiles on __main__ failed

Viewed 46

I have a script that reads a lot of csv files and puts them into a pandas dataframe for further processing. This script contains these two functions, where the second one calls the first one:

def read_csvfiles(date, folder_path, icon, info, file_suffix):
    d1 = pd.to_datetime(date)     
    if icon == 'icon':
        subfolder = '/'
    else:
        subfolder = '/G' + icon + str(d1.year)  
    df = pd.DataFrame(data=None)                                  
    for s in info:
        datestring = d1.strftime('%m-%d')
        file_path = folder_path + subfolder + str(d1.year) + f'-{datestring}-00/{s}' + file_suffix
        try:
            temp_df = pd.read_csv(file_path, sep=';', header=None, decimal='.', skiprows=1, index_col=0)
        except FileNotFoundError:
            print("File not found: " + subfolder + f'-{datestring}-00/{s}{file_suffix}, values were set to NaN')
            temp_df = pd.DataFrame(nan, columns=[s], index=pd.date_range(start=d1, periods=24 * 5 + 1, freq='H'))
        df[s] = temp_df.iloc[:, 0]
    if df.index.dtype != dtype('datetime64[ns]'):
        df.index = map(lambda num: datetime.datetime.strptime(str(num), '%d.%m.%Y %H:%M:%S'), df.index)
    return date, df


def read_fc(folder_path: str, icon: str, date_start: datetime.datetime, date_end: datetime.datetime, info: list, file_suffix: str) -> pd.Series:
    dates = pd.date_range(date_start, date_end, freq='d')
    pool = Pool()
    readfun = partial(read_csvfiles,
                      folder_path=folder_path,
                      icon=icon,
                      info=info,
                      file_suffix=file_suffix)
    data = pool.map(readfun, dates)
    return pd.Series(dict(data))

After the last windows update, using the second function after if __name__ == '__main__': gives me an error that didn't occure before, without changing anything in the script:

"_pickle.PicklingError: Can't pickle <function read_csvfiles at 0x0000024FB2C53488>: attribute lookup read_csvfiles on main failed"

Does anybody have any idea, why this error is occuring now and what I can do to fix it?

0 Answers
Related