Aidan L.
Aidan L.

Reputation: 89

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type Timestamp)

There a few similar ValueErrors questions, but none about Timestamp.

I am trying to predict the price of Bitcoin after 'n' days.

I converted to "Date" column to Timestamp. And when fitting the model, I need to convert the tensors to a Numpy Array. But I can't; this error occurs. Is there a way around it?

(Also, if I have any other errors, or something in the code that could be improve please tell me.)

I also feel like I am missing something. (I did cut out the preprocessing part, will do that later, I could use some advice on that too.)

Code:

import tensorflow as tf
import pandas as pd
import numpy as np

df = pd.read_csv('btcdata.csv', header=0, parse_dates=[0])
print(df)
target = df.pop('Close')
df = df.values

print()


model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(50, activation='relu', input_shape=(4,)))
model.add(tf.keras.layers.Lambda(
    lambda x: tf.expand_dims(model.output, axis=-1)))
model.add(tf.keras.layers.LSTM(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='relu'))
model.summary()
model.compile(optimizer='adam', loss='mean_absolute_error',
              metrics=['accuracy'])

model.fit(df, target, epochs=10)

Error:

Traceback (most recent call last):
  File "time-series-test.py", line 23, in <module>
    model.fit(df, target, epochs=10)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 815, in fit
    model=self)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1112, in __init__
    model=model)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 265, in __init__
    x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1013, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\util\nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\util\nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1008, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type Timestamp).

Upvotes: 0

Views: 1768

Answers (1)

William Clavier
William Clavier

Reputation: 334

I suggest you could try to remove the index from the DataFrame because that is what I am assuming contains the TimeStamp.

df.reset_index(drop=True, inplace=True) # Replaces the index with integers

Credit: https://kite.com/python/answers/how-to-drop-the-index-column-of-a-pandas-dataframe-in-python

Upvotes: 1

Related Questions