[Python] TypeError: 'str' object cannot be interpreted as an integer python for loop

Discussão em 'Python' iniciado por Stack, Agosto 3, 2025.

  1. Stack

    Stack Membro Participativo

    My function should return data starting from a given year and given month to today's year and month. I am getting error TypeError: 'str' object cannot be interpreted as an integer

    Defining function

    def frequency_database(df,start_month,start_year):
    data = pd.DataFrame([])
    import datetime
    start_month=int(start_month)
    start_year=int(start_year)
    today = datetime.date.today()
    today_year=today.strftime('%Y')

    for y in range(start_year,today_year):
    print('Outer loop enter for year', y)
    # Some function...
    for x in range(start_month,13):
    print('Inner loop enter for month number',x)
    # Some function...


    Calling funtion

    frequency_database(df,1,2015)


    Error

    TypeError: 'str' object cannot be interpreted as an integer

    Stack Trace as requested

    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-37-06567ae5d027> in <module>
    12 print('Inner loop enter for month number',x)
    13
    ---> 14 frequency_database(df,1,2015)

    <ipython-input-37-06567ae5d027> in frequency_database(df, start_month, start_year)
    7 today_year=today.strftime('%Y')
    8
    ----> 9 for y in range(start_year,today_year):
    10 print('Outer loop enter for year', y)
    11 for x in range(start_month,13):

    TypeError: 'str' object cannot be interpreted as an integer

    Continue reading...

Compartilhe esta Página