Pandas DataFrame datetime index doesn't survive JSON conversion and reconversion

L1meta

I have the following snippet of Python code:

import pandas as pd

# print normal index
print data.index

# convert from df to JSON and back
data_json = data.to_json()
df = pd.read_json(data_json)
df.index = pd.to_datetime(df.index)
print df.index

for some reason running this returns in:

<class 'pandas.tseries.index.DatetimeIndex'>
[1950-01-03 00:00:00, ..., 2014-08-21 00:00:00]
Length: 16264, Freq: None, Timezone: None
<class 'pandas.tseries.index.DatetimeIndex'>
[1966-10-31 00:00:00, ..., 2001-09-07 00:00:00]
Length: 16264, Freq: None, Timezone: None

Can someone explain to me what is going on and how I can have the index persist through the transformations?

chrisb

The error here is that to_json saves dates with ms resolution by defaul, while to_datetime converts with nanosecond resolution by default. To fix, either of these (but not both!) would work.

pd.to_datetime(df.index, unit='ms')
#OR
data_json = data.to_json(date_unit='ns')

As noted in comments, you can also just save the json with the dates in iso format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas dataframe datetime filter doesn't work

From Dev

Create datetime index in pandas dataframe

From Dev

Hydration object doesn't survive

From Dev

Pandas Dataframe datetime slicing with Index vs MultiIndex

From Dev

Interpolate and fill pandas dataframe with datetime index

From Dev

Find index of most recent DateTime in Pandas dataframe

From Dev

pandas multiple dataframe columns to single datetime index

From Dev

Pandas dataframe to json without index

From Dev

Pandas getting next available datetime index if desired one doesn't exist

From Dev

Conversion of string into datetime in pandas

From Dev

Pandas Conversion of datetime representation

From Dev

Pandas - conversion to datetime

From Dev

Datetime conversion issue pandas

From Dev

Can't index by timestamp in pandas dataframe

From Dev

Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

From Dev

Asserting that pandas dataframe has a datetime index through decorator

From Dev

Pandas - convert dataframe multi-index to datetime object

From Dev

How to access pandas DataFrame datetime index using strings

From Dev

Python pandas - pd.melt a dataframe with datetime index results in NaN

From Dev

Creating pandas dataframe with datetime index and random values in column

From Dev

Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

From Dev

Set multilevel index changes date to datetime in pandas dataframe

From Dev

Converting a Pandas datetime index on a DataFrame object to *MultiIndex* with the levels "month" and "year"

From Dev

Pandas dataframe - creating datetime index from separate columns

From Dev

Dart HttpRequest POST process doesn't survive function calls

From Dev

Kurtosis on groupby of pandas dataframe doesn't work

From Dev

datetime index in python dataframe

From Dev

Sort a pandas datetime index

From Dev

Timestamp conversion to datetime Python, Pandas

Related Related

  1. 1

    Pandas dataframe datetime filter doesn't work

  2. 2

    Create datetime index in pandas dataframe

  3. 3

    Hydration object doesn't survive

  4. 4

    Pandas Dataframe datetime slicing with Index vs MultiIndex

  5. 5

    Interpolate and fill pandas dataframe with datetime index

  6. 6

    Find index of most recent DateTime in Pandas dataframe

  7. 7

    pandas multiple dataframe columns to single datetime index

  8. 8

    Pandas dataframe to json without index

  9. 9

    Pandas getting next available datetime index if desired one doesn't exist

  10. 10

    Conversion of string into datetime in pandas

  11. 11

    Pandas Conversion of datetime representation

  12. 12

    Pandas - conversion to datetime

  13. 13

    Datetime conversion issue pandas

  14. 14

    Can't index by timestamp in pandas dataframe

  15. 15

    Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

  16. 16

    Asserting that pandas dataframe has a datetime index through decorator

  17. 17

    Pandas - convert dataframe multi-index to datetime object

  18. 18

    How to access pandas DataFrame datetime index using strings

  19. 19

    Python pandas - pd.melt a dataframe with datetime index results in NaN

  20. 20

    Creating pandas dataframe with datetime index and random values in column

  21. 21

    Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

  22. 22

    Set multilevel index changes date to datetime in pandas dataframe

  23. 23

    Converting a Pandas datetime index on a DataFrame object to *MultiIndex* with the levels "month" and "year"

  24. 24

    Pandas dataframe - creating datetime index from separate columns

  25. 25

    Dart HttpRequest POST process doesn't survive function calls

  26. 26

    Kurtosis on groupby of pandas dataframe doesn't work

  27. 27

    datetime index in python dataframe

  28. 28

    Sort a pandas datetime index

  29. 29

    Timestamp conversion to datetime Python, Pandas

HotTag

Archive