【发布时间】:2018-09-12 14:28:56
【问题描述】:
我正在尝试读取存储为 json 文件的推文。我正在使用熊猫来加载数据。但是在read_json 函数中发现了一些奇怪的行为。我在下面提供mcve:
json_content="""
{
"1": {
"tid": "9999999999999998",
},
"2": {
"tid": "9999999999999999",
},
"3": {
"tid": "10000000000000001",
},
"4": {
"tid": "10000000000000002",
}
}
"""
df=pd.read_json(json_content,
orient='index', # read as transposed
convert_axes=False, # don't convert keys to dates
)
print(df.info())
print(df)
在我的电脑上输出以下内容:
<class 'pandas.core.frame.DataFrame'>
Index: 4 entries, 1 to 4
Data columns (total 1 columns):
tid 4 non-null int64
dtypes: int64(1)
memory usage: 64.0+ bytes
None
tid
1 9999999999999998
2 10000000000000000
3 10000000000000000
4 10000000000000002
没有为
tid列存储正确的值,这是为什么 发生了什么?
注意:不应该有an overflow case。 tid 列存储为 int64,它的限制比我最初测试的 tid 高约 10 倍(见下文):
import sys
# original problem
tid_0 = 956677215197970432
print(sys.maxsize,tid_0,sys.maxsize/tid_0) # < 1 if overflow possible
# minimal case
tid = 10000000000000001
print(sys.maxsize,tid,sys.maxsize/tid) # < 1 if overflow possible
#Output
9223372036854775807 956677215197970432 9
9223372036854775807 10000000000000001 922
更新:
明确指定参数时读取正确
dtype=int,但我不明白为什么。当我们指定时会发生什么变化 数据类型?
【问题讨论】:
标签: python json python-3.x pandas