Search code examples
pythonpython-3.xpandasscikit-learntensorboard

Input contains infinity or a value too large for dtype('float64') error


Input contains infinity or a value too large for dtype('float64') error shows up when I run this code. How can I solve it?

from sklearn import preprocessing
from tensortrade.data.cdd import CryptoDataDownload 
import pandas as pd

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")

for col in data.columns:
  if col not in ['date', 'unix']:
    data[col]=data[col].pct_change()
    data.dropna(inplace=True)
    data[col] = preprocessing.scale(data[col].values)
    
print(data.head())

Solution

  • Try running this. You need to replace Nan and Inf values with a number of your choice

    import numpy as np
    from sklearn import preprocessing
    from tensortrade.data.cdd import CryptoDataDownload 
    import pandas as pd
    
    cdd = CryptoDataDownload()
    
    data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")
    
    for col in data.columns:
      if col not in ['date', 'unix']:
        data[col]=data[col].pct_change()
        data.dropna(inplace=True)
        print(data[col].tolist())
        # replace inf or nan with a number (for my example zero is selected)
        data[col] = [0 if np.isnan(x) or np.isinf(x) else x for x in data[col]]
    
        data[col] = preprocessing.scale(data[col].values)
        
    print(data.head())