I keep getting the following error regarding timestamp and float(). Suggestions? New to coding don't know where to add/remove parts of code to fix

Viewed 29
data=df.sort_index(ascending=True,axis=0)
new_dataset=pd.DataFrame(index=range(0,len(df)),columns=['Date','Close'])
new_dataset['Date'] = new_dataset['Date'].apply(pd.Timestamp.timestamp)

for i in range(0,len(data)):
    new_dataset["Date"][i]=data['Date'][i]
    new_dataset["Close"][i]=data["Close"][i]
    
scaler=MinMaxScaler(feature_range=(0,1))
final_dataset=new_dataset.values

train_data=final_dataset[0:987,:]
valid_data=final_dataset[987:,:]

new_dataset.index=new_dataset.Date
new_dataset.drop("Date",axis=1,inplace=True)
scaler=MinMaxScaler(feature_range=(0,1))
scaled_data=scaler.fit_transform(final_dataset)



x_train_data,y_train_data=[],[]

for i in range(60,len(train_data)):
    x_train_data.append(scaled_data[i-60:i,0])
    y_train_data.append(scaled_data[i,0])
    
x_train_data,y_train_data=np.array(x_train_data),np.array(y_train_data)

x_train_data=np.reshape(x_train_data,(x_train_data.shape[0],x_train_data.shape[1],1))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-d0e2fdf9e5c7> in <module>
     16 new_dataset.drop("Date",axis=1,inplace=True)
     17 scaler=MinMaxScaler(feature_range=(0,1))
---> 18 scaled_data=scaler.fit_transform(final_dataset)
     19 
     20 

~\Anaconda3\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params)
    688         if y is None:
    689             # fit method of arity 1 (unsupervised transformation)
--> 690             return self.fit(X, **fit_params).transform(X)
    691         else:
    692             # fit method of arity 2 (supervised transformation)

~\Anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in fit(self, X, y)
    334         # Reset internal state before fitting
    335         self._reset()
--> 336         return self.partial_fit(X, y)
    337 
    338     def partial_fit(self, X, y=None):

~\Anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in partial_fit(self, X, y)
    367 
    368         first_pass = not hasattr(self, 'n_samples_seen_')
--> 369         X = self._validate_data(X, reset=first_pass,
    370                                 estimator=self, dtype=FLOAT_DTYPES,
    371                                 force_all_finite="allow-nan")

~\Anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    418                     f"requires y to be passed, but the target y is None."
    419                 )
--> 420             X = check_array(X, **check_params)
    421             out = X
    422         else:

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    596                     array = array.astype(dtype, casting="unsafe", copy=False)
    597                 else:
--> 598                     array = np.asarray(array, order=order, dtype=dtype)
    599             except ComplexWarning:
    600                 raise ValueError("Complex data not supported\n"

TypeError: float() argument must be a string or a number, not 'Timestamp'

I am attempting to create a dashboard for stock prediction. Can't seem to get past this error. There seems to be an issue with converting a float to Timestamp. What can I do to convert the Timestamp to a float to get the code to run? The goal is to normalize the new filtered dataset. I get the error shown below after the code. Any help would be greatly appreciated!

1 Answers

The problem seems to be that scaler.fit_transform tries to scale the timestamp data which in your example is a timestamp object that can not be simply converted to float.

You could try to apply the float conversion on your data column before scaling it:

new_dataset['Date'] = new_dataset['Date'].apply(lambda x: float(pd.Timestamp(x).asm8)/10**9)

What this does is:

  • convert date string to pandas timestamp
  • convert to numpy nanoseconds
  • convert to float
  • divide by 10**9 to get the timestamp in seconds

In terms of efficiency there might be a better solution, I haven't tested that. If speed of conversion is an issue we can take a look at that.

Related