pandas.DataFrame.asof()不保留dtype

时间:2017-03-07 21:01:20

标签: pandas numpy

请考虑以下代码:

import numpy as np
import pandas as pd

N = 2 ** 60
x = np.array([1,2,3], dtype=np.int64)
y = np.array([N+1,N+2,N+3], dtype=np.int64)

df = pd.DataFrame({'x': x, 'y': y}).set_index('x')
df2 = df.asof(x+1)

print df.dtypes
print df2.dtypes

它产生:

y    int64
dtype: object
y    float64
dtype: object

为什么df2将dtype从int64更改为float64?我怎样才能把它变成int64呢?

请注意,简单地在astype()的输出上调用asof()并不好,因为int64-> float64-> int64往返会失去精度:

print df['y'].astype(np.int64)
print df2['y'].astype(np.int64)

的产率:

x
1    1152921504606846977
2    1152921504606846978
3    1152921504606846979
Name: y, dtype: int64
2    1152921504606846976
3    1152921504606846976
4    1152921504606846976
Name: y, dtype: int64

0 个答案:

没有答案