Pandas `read_json` 函数将字符串转换为 DateTime 对象,即使指定了 `convert_dates=False` 属性
Pandas `read_json` function converts strings to DateTime objects even the `convert_dates=False` attr is specified
我有下一个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)
时,我得到下一个结果:
如您所见,所有列都已自动转换。我用错了什么?
我一直在使用 python3.5 和 pandas 0.18.1
您需要 read_json
中的参数 convert_axes=False
:
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
如果值未转换为 index
或 columns
:,则 convert_dates=False
有效
[{
"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
如果同时使用两个参数:
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
我有下一个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)
时,我得到下一个结果:
如您所见,所有列都已自动转换。我用错了什么?
我一直在使用 python3.5 和 pandas 0.18.1
您需要 read_json
中的参数 convert_axes=False
:
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
如果值未转换为 index
或 columns
:,则 convert_dates=False
有效
[{
"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
如果同时使用两个参数:
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