I have written this code to process data. However, I am getting an error ‘float’ object has not attribute ‘rint’. This error occurs in line data_list = list(zip(days… however, other subsequent errors also occurred in the def average_array function… can anyone help me change the code to fix this error. Thanks.
import os
import pandas as pd
import numpy as np
import numpy.typing as npt
import regex as re
def get_date(file_name: str):
return re.findall(r'\d{2,4}', file_name)
def get_month_and_year(file_name: str):
date_stuff = get_date(file_name)
return date_stuff[1], date_stuff[2]
def do_mapping(index_array, year_list):
print(index_array)
return list(map(lambda x: year_list[int(x)], index_array))
def highest_lowest(array, index: int, year_list):
'''
I would imagine most of the bugs will happen here.
I'm not sure how nump
'''
high = np.nanmax(array[:, :, index], axis=0).astype(int)
high_index = np.nanargmax(array[:, :, index], axis=0)
print(high_index)
low = np.nanmin(array[:, :, index], axis=0).astype(int)
low_index = np.nanargmin(array[:, :, index], axis=0)
print(low_index)
low_year = do_mapping(low_index, year_list)
high_year = do_mapping(high_index, year_list)
return high, high_year, low, low_year
def average_array(array, index: int):
'''
I would imagine most of the bugs will happen here.
I'm not sure how nump
'''
average = np.around(np.nanmean(array[:, :, index], axis=0),decimals=0, out=None).astype(int)
return average
def process_array(array, year_list: list):
column_names = ['Day',
'Max High Temp', 'Max High Temp Year', 'Low Max Temp', 'Low High Temp Year',
'High Low Temp', 'High Low Year', 'Low Min Temp', 'Low Min Year',
'Max High Average Temp', 'Min Low Average Temp'
]
days = np.arange(1,array.shape[1]+1)
h_max, h_max_year, l_max, l_max_year = highest_lowest(array, 1, year_list)
h_min, h_min_year, l_min, l_min_year = highest_lowest(array, 2, year_list)
max_a= average_array(array,1)
min_a= average_array(array,2)
data_list = list(zip(days,
h_max, h_max_year, l_max, l_max_year,
h_min, h_min_year, l_min, l_min_year,
max_a, min_a
))
return pd.DataFrame(columns=column_names, data=data_list)
def main():
path = os.path.join(os.getcwd(), 'dataframes')
month_dict = {}
for file_name in os.listdir(path):
file_path = os.path.join(path, file_name)
print(file_path)
month, year = get_month_and_year(file_name)
current_dataframe = pd.read_csv(file_path, sep='\t')
try:
print(current_dataframe.to_numpy().shape)
print(month_dict[month][0].shape)
month_dict[month][0] = np.concatenate(
(month_dict[month][0], current_dataframe.to_numpy()[None]), axis=0)
month_dict[month][1].append(year)
except KeyError:
month_dict[month] = [current_dataframe.to_numpy()[None], [year]]
for month, data_tuple in month_dict.items():
data_frame = process_array(data_tuple[0], data_tuple[1])
final_file_name = f'final_dataframe_for_{month}.txt'
with open(os.path.join(path, final_file_name),'w') as f:
f.write('Meda 12 Temperature Climatology') # title of output file
now = pd.Timestamp.now().strftime('%m/%d/%Y, %H:%M:%S') # time stamp
f.write(now)
f.write('\n')
f.write(data_frame.to_string(justify='center',index=False))
if __name__ == "__main__":
main()