简体   繁体   English

AttributeError: 'int' 对象没有属性 'dtype'

[英]AttributeError: 'int' object has no attribute 'dtype'

I am trying to run a script to obtain data for a number of stocks.我正在尝试运行一个脚本来获取一些股票的数据。 Part of the data I am trying to obtain is a liquidity measure (called Amihud liquidity measure).我试图获得的部分数据是流动性指标(称为 Amihud 流动性指标)。 I automated the script but when running the automated script, I get an error after roughly 15-20 succesful returns.我自动化了脚本,但是在运行自动化脚本时,在大约 15-20 次成功返回后出现错误。 How can I fix this issue?我该如何解决这个问题?

File "script.py", line 23, in <module>
return_data = function.get_data(row[1], row[0])
File "C:\Users\leon_\function.py", line 39, in get_data
print(np.nanmean(illiq))
File "D:\Anaconda3\lib\site-packages\numpy\lib\nanfunctions.py", line 916, in nanmean
avg = _divide_by_count(tot, cnt, out=out)
File "D:\Anaconda3\lib\site-packages\numpy\lib\nanfunctions.py", line 190, in _divide_by_count
return a.dtype.type(a / b)
AttributeError: 'int' object has no attribute 'dtype'

The part of the code that handles the illiquidity measure:处理非流动性措施的代码部分:

  # Amihuds Liquidity measure
    liquidity_pricing_date = date_1 + datetime.timedelta(days=-20)
    liquidity_pricing_date2 = date_1 + datetime.timedelta(days=-120)
    stock_data = quandl.get(stock_ticker, start_date=liquidity_pricing_date2, end_date=liquidity_pricing_date)
    p = np.array(stock_data['Adj. Close'])
    returns = np.array(stock_data['Adj. Close'].pct_change())
    dollar_volume = np.array(stock_data['Volume'] * p)
    illiq = (np.divide(returns, dollar_volume))
    print(np.nanmean(illiq))
    illiquidity_measure = np.nanmean(illiq, dtype=float) * (10 ** 6)  # multiply by 10^6 for expositional purposes
    return [stock_vola, stock_price_average, illiquidity_measure]

Anyone has any idea on how to solve this?任何人都知道如何解决这个问题?

EDIT: This is the script file编辑:这是脚本文件

# Open File Dialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

# Load Spreadsheet data
f = open(file_path)

csv_f = csv.reader(f)
next(csv_f)

result_data = []

# Iterate
for row in csv_f:
    return_data = function.get_data(row[1], row[0])
    if len(return_data) != 0:
        # print(return_data)
        result_data_loc = [row[1], row[0]]
        result_data_loc.extend(return_data)
        result_data.append(result_data_loc)

if result_data is not None:
    with open('resuls.csv', mode='w', newline='') as result_file:
        csv_writer = csv.writer(result_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for result in result_data:
            # print(result)
            csv_writer.writerow(result)
else:
    print("No results found!")

[I'd place this as comment but given the length I can not] I don't feel like there's enough information for me to help you solve the issue, in your place, I would add this to make sure I understand why is the code failing and at the same time continue the process to finish it. [我会把它作为评论,但考虑到我不能的长度]我觉得没有足够的信息来帮助你解决问题,在你的地方,我会添加这个以确保我理解为什么代码失败,同时继续该过程以完成它。 This way you can then work on the files that failed and correct your script while still getting results.这样您就可以处理失败的文件并更正您的脚本,同时仍然获得结果。

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

# Load Spreadsheet data
f = open(file_path)

csv_f = csv.reader(f)
next(csv_f)

result_data = []

# Iterate
for row in csv_f:
    try:
       return_data = function.get_data(row[1], row[0])
       if len(return_data) != 0:
          # print(return_data)
          result_data_loc = [row[1], row[0]]
          result_data_loc.extend(return_data)
          result_data.append(result_data_loc)
    except AttributeError:
          print(row[0])
          print('\n\n')
          print(row[1])
          continue

if result_data is not None:
    with open('resuls.csv', mode='w', newline='') as result_file:
        csv_writer = csv.writer(result_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for result in result_data:
            # print(result)
            csv_writer.writerow(result)
else:
    print("No results found!")

So according to the traceback (thankfully we didn't have to ask for that), the error occurs in:所以根据回溯(幸好我们没有要求),错误发生在:

np.nanmean(illiq)

where it's trying to adjust the return value to match the dtype of an input, probably illiq .它正试图调整返回值以匹配dtype的输入,大概illiq At this point in nanmean (looking at its code) it has summed the input (after removing nan ), tot , and counted elements cnt .此时,在nanmean (查看其代码)中,它已对输入(删除nan )、 tot和计数元素cnt求和。 It's written assuming illiq is a numeric numpy array (preferably float dtype since it has to deal with float np.nan ).它的编写假设illiq是一个数字numpy数组(最好是float dtype,因为它必须处理 float np.nan )。

So it works most of the time, but in some cases fails.所以它大部分时间都有效,但在某些情况下会失败。 What is different about illiq in those cases?在这些情况下, illiq什么不同?

p = np.array(stock_data['Adj. Close'])
returns = np.array(stock_data['Adj. Close'].pct_change())
dollar_volume = np.array(stock_data['Volume'] * p)
illiq = (np.divide(returns, dollar_volume))

Looks like stock_data is a dataframe , and the inputs are arrays derived from individual series .看起来stock_data是一个dataframe stock_data ,输入是从单个series派生的数组。 I believe stock_data[name].to_num() is the preferred way of getting an array from a Series, though np.array(...) may work most of the time.我相信stock_data[name].to_num()是从系列中获取数组的首选方式,尽管np.array(...)可能大部分时间都可以工作。 stock_data[name].values was also used.还使用了stock_data[name].values

I'd suggest applying some tests to illiq before this call.我建议在此调用之前对illiq应用一些测试。 Check shape and dtype at least.至少检查shapedtype Try to identify what's different in the problem case.尝试找出问题案例中的不同之处。

Here's a simple case that produces this error:这是一个产生此错误的简单案例:

In [117]: np.nanmean(np.array([0,3],object))                                                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-117-26ab42d92ec9> in <module>
----> 1 np.nanmean(np.array([0,3],object))

<__array_function__ internals> in nanmean(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py in nanmean(a, axis, dtype, out, keepdims)
    949     cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=keepdims)
    950     tot = np.sum(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
--> 951     avg = _divide_by_count(tot, cnt, out=out)
    952 
    953     isbad = (cnt == 0)

/usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py in _divide_by_count(a, b, out)
    216         else:
    217             if out is None:
--> 218                 return a.dtype.type(a / b)
    219             else:
    220                 # This is questionable, but currently a numpy scalar can

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

pandas often creates object dtype Series when one or more values is not a valid number.当一个或多个值不是有效数字时, pandas通常会创建对象 dtype 系列。 That can include strings and None values.这可以包括字符串和None值。

The simple answer is that your data is not a numpy datatype.简单的答案是您的数据不是 numpy 数据类型。 This is likely because the column isn't fully numeric (ie contains None or something).这可能是因为该列不是完全数字(即包含 None 或其他内容)。

The short solution:简短的解决方案:

print(np.nanmean(pd.to_numeric(illiq)))

The quickest way to solve this is to simply coerce the data to a numeric type that numpy likes.解决这个问题的最快方法是简单地将数据强制转换为 numpy 喜欢的数字类型。 This can be done via pandas' to_numeric method.这可以通过熊猫的to_numeric方法来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:类型对象&#39;object&#39;没有属性&#39;dtype&#39; - AttributeError: type object 'object' has no attribute 'dtype' AttributeError:“ TensorSliceDataset”对象没有属性“ dtype” - AttributeError: 'TensorSliceDataset' object has no attribute 'dtype' AttributeError:“ NoneType”对象没有属性“ dtype” - AttributeError: 'NoneType' object has no attribute 'dtype' AttributeError: 'df' object 没有属性 'dtype' - AttributeError: 'df' object has no attribute 'dtype' AttributeError:“列表”对象没有属性“ dtype” - AttributeError: 'list' object has no attribute 'dtype' AttributeError:&#39;CommandQueue&#39;对象没有属性&#39;dtype&#39; - AttributeError: 'CommandQueue' object has no attribute 'dtype' AttributeError:int对象没有属性 - AttributeError : int object has no attribute 估算器API:AttributeError:&#39;NoneType&#39;对象没有属性&#39;dtype&#39; - Estimator API: AttributeError: 'NoneType' object has no attribute 'dtype' 熊猫read_csv:AttributeError:“ NoneType”对象没有属性“ dtype” - Pandas read_csv: AttributeError: 'NoneType' object has no attribute 'dtype' “AttributeError: &#39;DataFrame&#39; 对象没有属性 &#39;dtype&#39;” 使用 Pandas to_datetime - "AttributeError: 'DataFrame' object has no attribute 'dtype'" using pandas to_datetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM