TypeError: reduction operation 'argmin' not allowed for this dtype

TypeError: reduction operation 'argmin' not allowed for this dtype

使用pd.read_sql() 方法把Mysql的数据库转为dataframe后,使用 df['low'].idxmin() 或者df['low'].argmin() 方法是会出现这个错误。
 
date = date + '-01-01'
        cmd = 'select * from `{}` where datetime > \'{}\''.format(code, date)

        try:
            df = pd.read_sql(cmd, history_engine,index_col='index')
        except Exception,e:
            print e
            return None
        
        # 不知道为啥,这里的类型发生改变
        idx= df['low'].idxmin()
 
 
TypeError: reduction operation 'argmin' not allowed for this dtype
 
把df的每列数据类型打印出来看看。
 
print df.dtypes
 
datetime    datetime64[ns]
code                object
name                object
open                object
close               object
high                object
low                 object
vol                float64
amount             float64
dtype: object

晕,居然把low这些float类型全部转为了object,所以解决办法就是把这些列的数据转为float64.
 
df['low']=df['low'].astype('float64')

这样之后,问题就解决了。

猜你喜欢

转载自blog.csdn.net/Aguangg_6655_la/article/details/79599739