The original form of date in dataframe is:
Date
2018-09-17 12.83 12.92 12.38 12.65 12.65 1937329.0
2018-09-10 12.92 13.12 12.81 12.83 12.83 1150470.0
After converted to json, df.to_json(orient='index',date_format='iso')
it looks like this:
"2018-09-17T00:00:00Z":{"
any way to fix this?
Please notice that you can also specify the output date format other than the default one, by using the dt. strftime() method. For example, you can choose to display the output date as MM/DD/YYYY by specifying dt. strftime('%m/%d/%Y') .
Use astype() to Change datetime to String Format You can use this if the date is already in the format you want it in string form. The below example returns the date as a string with format %Y/%m/%d . dtype of column ConvertedDate will be object ( string ). Yields below output.
Function usedstrftime() can change the date format in python.
The easiest fix is to first convert your datetime
series to an object
dtype series of strings:
df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')
I advise you only do this as a final step prior to json conversion, as you lose benefits of vectorised computations and likely will see less efficient memory usage.
the first one is in case you get an error in this part ".dt.strftime('%m-%d-%Y')"
df['date'] = pd.to_datetime(df['date'], errors='coerce')
df['date'] = df['date'].dt.strftime('%m-%d-%Y')
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