pandas 的read_json函数将字符串转换为DateTime对象,即使指定了`convert_dates = False` attr [英] Pandas `read_json` function converts strings to DateTime objects even the `convert_dates=False` attr is specified

查看:124
本文介绍了 pandas 的read_json函数将字符串转换为DateTime对象,即使指定了`convert_dates = False` attr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个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
}]

当我呼叫pandas.read_json(JSON, convert_dates=False)时,我得到下一个结果:

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

如您所见,所有列均已自动转换.我用错了什么?

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

我一直在使用python3.5和pandas 0.18.1

I've been using python3.5 and pandas 0.18.1

推荐答案

您需要在convert_axes=False .html"rel =" nofollow> read_json :

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

如果值未转换为indexcolumns,则

convert_dates=False有效:

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

这篇关于 pandas 的read_json函数将字符串转换为DateTime对象,即使指定了`convert_dates = False` attr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆