合并将时间戳转换为科学记数法并失去精度
Merge converting timestamp to scientific notation and losing precision
原始时间戳 dtype int64
ts = datetime.fromtimestamp(1627741304932/1000)
print(ts)
2021-07-31 17:21:44.932000
合并数据帧后,时间戳 loses/gains +-5 分钟,dtype 变为 float64
ts = datetime.fromtimestamp(1.627741e+12/1000)
print(ts)
2021-07-31 17:16:40
有没有办法避免这种转换或至少避免精度损失?
除了丢掉万亿+合并后归还?
更新
我已经为我的问题创建了一个确切的例子:
例子
df1 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts4'],
'col2': [1627741304932, 1627741304931, 1627741304930, 1627741304929]})
df2 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts5'],
'col2': [1627741305932, 1627741304931, 1627741304930, 1627741304920]})
x = df1.merge(df2, on='col1', how='outer', suffixes=('_prev', '_new'))
print(x)
print(x.dtypes)
输出
发生这种情况是因为在合并期间将 NaN 值添加到数据框中
col1 col2_prev col2_new
0 ts1 1.627741e+12 1.627741e+12
1 ts2 1.627741e+12 1.627741e+12
2 ts3 1.627741e+12 1.627741e+12
3 ts4 1.627741e+12 NaN
4 ts5 NaN 1.627741e+12
col1 object
col2_prev float64
col2_new float64
dtype: object
我该如何解决这个问题?
所以问题似乎归结为 pandas 将时间戳从 int 转换为 float。这是因为 'int64' 数据类型不支持 NaN 值。
为了克服这个问题,我们可以使用 Nullable integer data types:
例如:
df1 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts4'],
'col2': [1627741304932, 1627741304931, 1627741304930, 1627741304929]})
df2 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts5'],
'col2': [1627741305932, 1627741304931, 1627741304930, 1627741304920]})
# allow NaN values (notice the capital I)
df1['col2'] =df1['col2'].astype('Int64')
df2['col2'] =df2['col2'].astype('Int64')
x = df1.merge(df2, on='col1', how='outer', suffixes=('_prev', '_new'))
print(x)
print(x.dtypes)
输出:
col1 col2_prev col2_new
0 ts1 1627741304932 1627741305932
1 ts2 1627741304931 1627741304931
2 ts3 1627741304930 1627741304930
3 ts4 1627741304929 <NA>
4 ts5 <NA> 1627741304920
col1 object
col2_prev Int64
col2_new Int64
dtype: object
原始时间戳 dtype int64
ts = datetime.fromtimestamp(1627741304932/1000)
print(ts)
2021-07-31 17:21:44.932000
合并数据帧后,时间戳 loses/gains +-5 分钟,dtype 变为 float64
ts = datetime.fromtimestamp(1.627741e+12/1000)
print(ts)
2021-07-31 17:16:40
有没有办法避免这种转换或至少避免精度损失?
除了丢掉万亿+合并后归还?
更新
我已经为我的问题创建了一个确切的例子:
例子
df1 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts4'],
'col2': [1627741304932, 1627741304931, 1627741304930, 1627741304929]})
df2 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts5'],
'col2': [1627741305932, 1627741304931, 1627741304930, 1627741304920]})
x = df1.merge(df2, on='col1', how='outer', suffixes=('_prev', '_new'))
print(x)
print(x.dtypes)
输出
发生这种情况是因为在合并期间将 NaN 值添加到数据框中
col1 col2_prev col2_new
0 ts1 1.627741e+12 1.627741e+12
1 ts2 1.627741e+12 1.627741e+12
2 ts3 1.627741e+12 1.627741e+12
3 ts4 1.627741e+12 NaN
4 ts5 NaN 1.627741e+12
col1 object
col2_prev float64
col2_new float64
dtype: object
我该如何解决这个问题?
所以问题似乎归结为 pandas 将时间戳从 int 转换为 float。这是因为 'int64' 数据类型不支持 NaN 值。
为了克服这个问题,我们可以使用 Nullable integer data types:
例如:
df1 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts4'],
'col2': [1627741304932, 1627741304931, 1627741304930, 1627741304929]})
df2 = pd.DataFrame({'col1': ['ts1', 'ts2', 'ts3', 'ts5'],
'col2': [1627741305932, 1627741304931, 1627741304930, 1627741304920]})
# allow NaN values (notice the capital I)
df1['col2'] =df1['col2'].astype('Int64')
df2['col2'] =df2['col2'].astype('Int64')
x = df1.merge(df2, on='col1', how='outer', suffixes=('_prev', '_new'))
print(x)
print(x.dtypes)
输出:
col1 col2_prev col2_new
0 ts1 1627741304932 1627741305932
1 ts2 1627741304931 1627741304931
2 ts3 1627741304930 1627741304930
3 ts4 1627741304929 <NA>
4 ts5 <NA> 1627741304920
col1 object
col2_prev Int64
col2_new Int64
dtype: object