简体   繁体   中英

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

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

and then you can convert your variable to integer type

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