简体   繁体   中英

AttributeError: 'int' object has no attribute 'to_dict'

I have this issue with my code. I have a list of strings, symbols , that I pass as a parameter in my function, statsTable() .

I'm using the symbols list in my SQL query to pull the requested items from my dB, in my case this list is a list of stocks. I would rather not use a loop here. The output of the function should create a DataFrame using data from my PostgreSQL dB.

Take a look below:

@app.callback(
dash.dependencies.Output('table_stats', 'data'),
[dash.dependencies.Input('dynamic-dropdown', 'value')])

def statsTable(symbols):
    if symbols == None:
        raise PreventUpdate

    symbols = tuple(symbols)

    df = postgresql_to_dataframe(conn, f"SELECT id, companyname, marketcap, to_char(100.0*week52change,'999D99%'), week52high, week52low, to_char(dividend_yield * 100, '99D99%'), next_earnings_date, pe_ratio, ROUND(beta,2) FROM security_stats WHERE security_stats.id IS {symbols} ;", col)
    data = df.to_dict('rows')
    columns=[{"name": i, "id": i} for i in security_stats.columns]
    return dt.DataTable(data=data, columns=columns)

I transform the list of stocks into a Tuple to remove the brackets so my SQL query doesn't get confused. When I run this code for my Dash app, I get AttributeError: 'int' object has no attribute 'to_dict' .

I don't understand why because when I run the code below in a sandbox environment (to test if it works) everything seems fine, I get a DataFrame of the data that I'm asking for , without using a loop:

symbols = ('FB','AAPL', 'EBAY','AMD')
col = ['Symbol','Company','Market Cap','1yr-Change', '52-Week High','52-Week Low','Dividend Yield','Next Earnings Report','PE-Ratio','Beta']

df = postgresql_to_dataframe(conn, f"SELECT id, companyname, marketcap, to_char(100.0*week52change,'999D99%'), week52high, week52low, to_char(dividend_yield * 100, '99D99%'), next_earnings_date, pe_ratio, ROUND(beta,2) FROM security_stats WHERE security_stats.id IN {symbols} ;", col)
df.head()

This is the output I'm looking for...

数据框

If this helps, here is the function I use to pull the SQL data and transform it to a DataFrame:

def postgresql_to_dataframe(conn, select_query, column_names):
    """
    Tranform a SELECT query into a pandas dataframe
    """
    cursor = conn.cursor()
    try:
        cursor.execute(select_query)
    except (Exception, psycopg2.DatabaseError) as error:
        print("Error: %s" % error)
        cursor.close()
        return 1
    
    # Naturally we get a list of tupples
    tupples = cursor.fetchall()
    cursor.close()
    
    # We just need to turn it into a pandas dataframe
    df = pd.DataFrame(tupples, columns=column_names)
    return df

What is causing me to raise this error AttributeError: 'int' object has no attribute 'to_dict' in my statsTable(symbols) function? I appreciate all the help I can get here, been struggling with this one.

EDIT: This is the full error.

>>> data = df.to_dict('rows')
AttributeError: 'int' object has no attribute 'to_dict'
try:
    cursor.execute(select_query)
except (Exception, psycopg2.DatabaseError) as error:
    print("Error: %s" % error)
    cursor.close()
    return 1

When the exception is raised, your exception handling causes postgresql_to_dataframe to return an integer rather than a DataFrame. It is, of course, not possible to call .to_dict on an integer, as the error message tells you quite explicitly.

So the proximate cause is that an exception is raised by the query execution. The Dash app is probably a context that doesn't allow you to see the standard output, so you aren't getting the error printout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM