简体   繁体   中英

TypeError when using pandas DataFrame.idxmax()

My code (using pandas, pandas_datareader)

info = robinhood.RobinhoodHistoricalReader("AAPL", start=start, end=end, interval="5minute", span="day")
prices = pd.DataFrame(info.read(), columns=['close_price'])
minutes = prices.idxmax(axis=0) * 5

Gives a TypeError: reduction operation 'argmax' not allowed for this dtype

Any idea how to fix this? I just want to find the index of the max price in my dataset.

Try this:

info = robinhood.RobinhoodHistoricalReader("AAPL", start=start, end=end, interval="5minute", span="day")
prices = pd.DataFrame(info.read(), columns=['close_price'])
minutes = prices.astype(float).idxmax(axis=0) * 5

Assumes your row index is numeric. minutes is now a row showing the row_index*5 for the max cell value in each column.

在我的情况下,检查是否有任何数据进入数据框,因为没有数据,并出现此错误

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