discort discort - 2 months ago 10
JSON Question

Pandas `read_json` function converts strings to DateTime objects even the `convert_dates=False` attr is specified

I have the next JSON:

[{
"2016-08": 1355,
"2016-09": 2799,
"2016-10": 2432,
"2016-11": 0
}, {
"2016-08": 1475,
"2016-09": 1968,
"2016-10": 1375,
"2016-11": 0
}, {
"2016-08": 3097,
"2016-09": 1244,
"2016-10": 2339,
"2016-11": 0
}, {
"2016-08": 1305,
"2016-09": 1625,
"2016-10": 3038,
"2016-11": 0
}, {
"2016-08": 1530,
"2016-09": 4385,
"2016-10": 2369,
"2016-11": 0
}, {
"2016-08": 3515,
"2016-09": 4532,
"2016-10": 2497,
"2016-11": 0
}, {
"2016-08": 1539,
"2016-09": 1276,
"2016-10": 4378,
"2016-11": 0
}, {
"2016-08": 4989,
"2016-09": 3143,
"2016-10": 2075,
"2016-11": 0
}, {
"2016-08": 3357,
"2016-09": 2745,
"2016-10": 1592,
"2016-11": 0
}, {
"2016-08": 3224,
"2016-09": 2694,
"2016-10": 3958,
"2016-11": 0
}]


When I call
pandas.read_json(JSON, convert_dates=False)
I get the next result:

enter image description here

As you can see all columns have been converted automatically. What do I use the wrong?

I've been using python3.5 and pandas 0.18.1

Answer

You need parameter convert_axes=False in read_json:

df = pd.read_json('file.json', convert_axes=False)
print (df)
   2016-08  2016-09  2016-10  2016-11
0     1355     2799     2432        0
1     1475     1968     1375        0
2     3097     1244     2339        0
3     1305     1625     3038        0
4     1530     4385     2369        0
5     3515     4532     2497        0
6     1539     1276     4378        0
7     4989     3143     2075        0
8     3357     2745     1592        0
9     3224     2694     3958        0

convert_dates=False works if value is not converted to index or columns:

[{
    "2016-08": "2016-08",
    "2016-09": 2799,
    "2016-10": 2432,
    "2016-11": 0
}, {
    "2016-08": 1475,
    "2016-09": 1968,
    "2016-10": 1375,
    "2016-11": 0
},
...
...

#1355 changed to '2016-08'
df = pd.read_json('file.json', convert_dates=False)
print (df)
  2016-08-01  2016-09-01  2016-10-01  2016-11-01
0    2016-08        2799        2432           0
1       1475        1968        1375           0
2       3097        1244        2339           0
3       1305        1625        3038           0
4       1530        4385        2369           0
5       3515        4532        2497           0
6       1539        1276        4378           0
7       4989        3143        2075           0
8       3357        2745        1592           0
9       3224        2694        3958           0

If use both parameters:

df = pd.read_json('file.json', convert_dates=False, convert_axes=False)
print (df)
   2016-08  2016-09  2016-10  2016-11
0  2016-08     2799     2432        0
1     1475     1968     1375        0
2     3097     1244     2339        0
3     1305     1625     3038        0
4     1530     4385     2369        0
5     3515     4532     2497        0
6     1539     1276     4378        0
7     4989     3143     2075        0
8     3357     2745     1592        0
9     3224     2694     3958        0
Comments