Python error : TypeError: Object of type 'Timestamp' is not JSON serializable'

Question:

I have a Dataframe that has a time stamp column of type ‘datetime64[ns]’. When I try to insert it to Salesforce platform get an error ‘TypeError: Object of type ‘Timestamp’ is not JSON serializable‘. How could I change this timestamp column to have it updated properly. Given below is the view of the Dataframe.

Id,Name,Date,Type
1,ProdA,2018-05-18 04:45:08,S
1,ProdB,2018-05-18 02:15:00,S
1,ProdC,2018-05-16 10:20:00,S

Datatype for each of these 4 columns:

Id                                     object
Name                                   object
Date                           datetime64[ns]
Type                                   object
dtype: object

Could anyone assist on this. Thanks.

Asked By: dark horse

||

Answers:

You can try convert datetime to string:

df['Date'] = df['Date'].astype(str)

Or:

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d %H:%M:%S')

print (df.dtypes)
Id      object
Name    object
Date    object
Type    object
dtype: object
Answered By: jezrael

If you get an error TimeStamp has no attribute as "astype(str)", you can try, for example, str(timeseries.index[0]). This will convert the timestamp into a string that can then be serialized.

Answered By: Muhammad Ali
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.