Search code examples
pythonnumpydatetimetensorpython-datetime

Timestamp : Failed to convert a NumPy array to a Tensor


I have pandas dataframe that looks like this:

time                                value
2019-05-24 04:15:35.742000+00:00    -0.085714

At one point of my code when I try to do this:

hist = model.fit(
    X_train, y_train, 
...
)

where X_train is derived from the dataframe and looks like :

array([[[Timestamp('2019-05-21 14:16:37.091000'), -0.22857142857142856,          1.3553382233088835],

I get the following error:

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

Edit:

tr['execution_time'] = pd.to_datetime(tr.execution_time).dt.tz_localize(None) 

This also didn't help.


Solution

  • First we have to convert it to datetime object.

    df['execution_time'] = pd.to_datetime(df.execution_time).dt.tz_localize(None)
    

    After that we have to convert datetime object to float value using timestamp() function

    for i in range(len(df)):
      df['execution_time'][i]=df['execution_time'][i].timestamp()
    

    After that we can convert the values to float.

    df = df.astype('float32')
    

    And then it can be converted to tensors easily.