简体   繁体   English

获取无效的类型比较错误

[英]Getting invalid type comparison error

I am getting invalid type comparison error if anyone could help? 如果有人可以帮助,我会收到无效的类型比较错误? Basically, I am getting an error on the line where I want to replace all "-" with zeros in the data frame, so as to make it uniform for numerical manipulations. 基本上,我在想用数据框中的零替换所有“-”的行上遇到错误,以便使其统一用于数值操作。 Following is my code and error respectively: 以下分别是我的代码和错误:

Code: 码:

import quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

import time
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
from matplotlib import style
style.use('ggplot')

# get market info for bitcoin from the start of 2016 to the current day
bitcoin_market_info = pd.read_html("https://coinmarketcap.com/currencies/ethereum/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))[0]
# convert the date string to the correct date format
bitcoin_market_info = bitcoin_market_info.assign(Date=pd.to_datetime(bitcoin_market_info['Date']))
# when Volume is equal to '-' convert it to 0
#In the line below I am getting error
bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0
# convert to int
bitcoin_market_info['Volume'] = bitcoin_market_info['Volume'].astype('int64')

bitcoin_market_info.set_index('Date', inplace=True)
bitcoin_market_info = bitcoin_market_info.reindex(index=bitcoin_market_info.index[::-1])
# look at the first few rows
bitcoin_market_info.head()

Error: 错误:

F:\Anaconda3\lib\site-packages\pandas\core\ops.py:798: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
Traceback (most recent call last):
File "C:/Users/The_Anil/Desktop/fvsffsf.py", line 20, in <module>
bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0
File "F:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 861, in wrapper
res = na_op(values, other)
File "F:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 800, in na_op
raise TypeError("invalid type comparison")
TypeError: invalid type comparison

Problem is here 问题在这里

bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0

You are comparing it with a string 您正在将其与字符串进行比较

bitcoin_market_info['Volume']=="-"

as if the variable is of string type and then trying to assign a integer type value to same variable at the end of it. 就像变量是字符串类型,然后尝试在其末尾将整数类型值分配给同一变量。

I am guessing your variable Volume is string type so you can do this 我猜你的变量Volume是字符串类型,所以你可以这样做

bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']="0"

and then you can convert your variable to integer type 然后可以将变量转换为整数类型

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

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