Error with RINT

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()

Please post the full traceback so that we can see where the exception was raised.

AttributeError: ‘float’ object has no attribute ‘rint’

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

Traceback (most recent call last):
line 59, in _wrapfunc
return bound(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

During handling of the above exception, another exception occurred:

AttributeError: ‘float’ object has no attribute ‘rint’

That traceback is very short. I would have expect a few more lines of output.

That means that you called bound() with the wrong argument type.
Suggest you read the documentation for bound() to find out what is expected.

I cut off some of the traceback.

Where did I mess up is the question?

We can’t tell because:

:wink:

The part you cut off is the part that shows where in your code the exception was raised. The last few lines only show function calls that are internal to the libraries you’re using. We’d need to see the earlier lines to know which line in your code the exception is coming from. See 8. Errors and Exceptions — Python 3.12.5 documentation for more details on how exception tracebacks are formatted.

Can you post the entire exception traceback (as formatted text)?

AttributeError: ‘float’ object has no attribute ‘rint’

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

Traceback (most recent call last):
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 59, in _wrapfunc
return bound(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

During handling of the above exception, another exception occurred:

AttributeError: ‘float’ object has no attribute ‘rint’

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

Traceback (most recent call last):
Fwx_climo\Lib\runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\runpy.py", line 88, in run_code
exec(code, run_globals)
.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy_main
.py", line 39, in
cli.main()
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py", line 430, in main
run()
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name=“main”)
.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
wx_climo\Final_product.py", line 95, in
main()
wx_climo\Final_product.py", line 84, in main
data_frame = process_array(data_tuple[0], data_tuple[1])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Final_product.py", line 57, in process_array
max_a= average_array(array,1)
^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Final_product.py", line 43, in average_array
average = np.around(np.nanmean(array[:, :, index], axis=0),decimals=0, out=None).astype(int)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 3377, in around
return _wrapfunc(a, ‘round’, decimals=decimals, out=out)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 68, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 45, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

Please format as preformatted text, as you did for your code, so that traceback is easier to read.

AttributeError: ‘float’ object has no attribute ‘rint’

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

Traceback (most recent call last):
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 59, in _wrapfunc
return bound(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

During handling of the above exception, another exception occurred:

AttributeError: ‘float’ object has no attribute ‘rint’

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

Traceback (most recent call last):
Fwx_climo\Lib\runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\runpy.py", line 88, in run_code
exec(code, run_globals)
.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy_main.py", line 39, in
cli.main()
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py", line 430, in main
run()
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name=“main”)
.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
wx_climo\Final_product.py", line 95, in
main()
wx_climo\Final_product.py", line 84, in main
data_frame = process_array(data_tuple[0], data_tuple[1])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Final_product.py", line 57, in process_array
max_a= average_array(array,1)
^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Final_product.py", line 43, in average_array
average = np.around(np.nanmean(array[:, :, index], axis=0),decimals=0, out=None).astype(int)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 3377, in around
return _wrapfunc(a, ‘round’, decimals=decimals, out=out)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 68, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wx_climo\Lib\site-packages\numpy\core\fromnumeric.py", line 45, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

I was able to figure out the code. It was a minor issue with my data files. Thank you for trying and being there.