Home > OS >  TypeError: cannot convert the series to <class 'float'> in pandas
TypeError: cannot convert the series to <class 'float'> in pandas

Time:10-15

I have the following code which tests if a stock makes new lifetime high. Bool output should be generated with numpy comparison.

import pandas as pd
from pandas_datareader import data as web
import numpy as np
import math

data = web.DataReader('goog', 'yahoo')
data['lifetime'] = data['High'].asfreq('D').rolling(window=999999, min_periods=1).max() #To check if it is a lifetime high
data['lifetime'] = data['lifetime'].astype(float)
data['isclose'] = np.where(math.isclose(data['High'], data['lifetime'], rel_tol = 0.003), 1, 0)

This solution which was offered here iterating over each row in pandas to evaluate condition, should work but i am getting this error.

TypeError: cannot convert the series to <class 'float'>

I did some looking and it seems that math function is expecting a single value and not an array. How can i verify that it is the case? TypeError: cannot convert the series to <class 'float'>

CodePudding user response:

Use numpy.isclose for working with arrays, here columns:

data['isclose'] = np.where(np.isclose(data['High'], data['lifetime'], rel_tol = 0.003), 1, 0)
  • Related