Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Pandas Dataframe read_json method convert my epoch to human readable string

I use to_json method to serialize my dataframe, the content is look like this:

"1467065160244362165":"1985.875","1467065161029130301":"1985.875","1467065161481601498":"1985.875","1467065161486508221":"1985.875"

How do I stop the read_json method convert my epoch value from 1467065160244362165 to something like 2016-06-28 06:57:23.786726222. This is how i call read_json:

data = pd.read_json(remote_result_fullpath, convert_dates=False)
like image 877
Bryan Fok Avatar asked Sep 05 '16 12:09

Bryan Fok


1 Answers

For me works:

import pandas as pd

#added {} to file
remote_result_fullpath = 'https://dl.dropboxusercontent.com/u/84444599/file.json'

data = pd.read_json(remote_result_fullpath, 
                    convert_dates=False, #dont convert columns to dates 
                    convert_axes=False, #dont convert index to dates
                    typ='series') #if need convert output to Series

print (data)
1467065160244362165    1985.875
1467065161029130301    1985.875
1467065161481601498    1985.875
1467065161486508221    1985.875

print (data.dtypes)
dtype: float64
float64

If need strings add dtype:

data = pd.read_json(remote_result_fullpath, 
                    convert_dates=False, 
                    convert_axes=False,
                    typ='series',
                    dtype='object')

print (data)
1467065160244362165    1985.875
1467065161029130301    1985.875
1467065161481601498    1985.875
1467065161486508221    1985.875

print (data.dtypes)
dtype: object
object

print (data.index)
Index(['1467065160244362165', '1467065161029130301', '1467065161481601498',
       '1467065161486508221'],
      dtype='object')
like image 123
jezrael Avatar answered Oct 28 '22 14:10

jezrael