14

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?

3 Answers 3

23

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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')

Comments

0

You can also use this alternative:

df = pd.read_json('test.json')
df['date_hour'] = [datetime.strptime(date[0:10],'%Y-%m-%d').date() for date in 
df['date_hour']]

You can read more in my answer on a similar topic - https://stackoverflow.com/a/56280982/11222127

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.