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.
The Python "TypeError: Object of type datetime is not JSON serializable" occurs when we try to convert a datetime object to a JSON string. To solve the error, set the default keyword argument to str in your call to the json. dumps() method.
Serialize datetime by converting it into String You can convert dateTime value into its String representation and encode it directly, here you don't need to write any encoder. We need to set the default parameter of a json. dump() or json. dumps() to str like this json.
Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With