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.
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
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.
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.
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
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.