我有一個索引為 3 到 15 的 pandas 資料框,步長為 0.5,并且希望將其重新索引為 0.1 步。我嘗試了這段代碼,但它不起作用
# create data and set index and print for verification
df = pd.DataFrame({'A':np.arange(3,5,0.5),'B':np.arange(3,5,0.5)})
df.set_index('A', inplace = True)
df.reindex(np.arange(3,5,0.1)).head(15)
上面的代碼輸出這個:
| 一種 | 乙 |
|---|---|
| 3.0 | 3.0 |
| 3.1 | 鈉 |
| 3.2 | 鈉 |
| 3.3 | 鈉 |
| 3.4 | 鈉 |
| 3.5 | NaN * 此位置的預期輸出為 3.5,因為它存在于原始 df 中 |
| 3.6 | 鈉 |
| 3.7 | 鈉 |
| 3.8 | 鈉 |
奇怪的是,從 0 而不是 3 重新索引時,問題得到了解決,如下面的代碼所示:
df = pd.DataFrame({'A':np.arange(3,5,0.5),'B':np.arange(3,5,0.5)})
df.set_index('A', inplace = True)
print(df.head())
df.reindex(np.arange(0,5,0.1)).head(60)
輸出現在正確顯示
| 一種 | 乙 |
|---|---|
| 0.0 | 鈉 |
| ... | ... |
| 3.0 | 3.0 |
| 3.1 | 鈉 |
| 3.2 | 鈉 |
| 3.3 | 鈉 |
| 3.4 | 鈉 |
| 3.5 | 3.5 |
| 3.6 | 鈉 |
| 3.7 | 鈉 |
| 3.8 | 鈉 |
我在 Windows 10 上運行 python 3.8.5。
熊貓版本是 1.4.07
Numpy 版本是 1.22.1
有誰知道為什么會這樣?如果它是一個已知的或新的錯誤?如果該錯誤已在較新版本的 python、pandas 或 numpy 中修復?
謝謝
uj5u.com熱心網友回復:
好問題。
答案是因為np.arange(3,5,0.1)創建了一個不完全是 3.5 的 3.5 值。它是 3.5000000000000004。但np.arange(0,5,0.1)確實創建了一個 3.5,它正好是 3.5。另外,np.arange(3,5,0.5)還會生成一個 3.5,也就是 3.5。
pd.Index(np.arange(3,5,0.1))
Float64Index([ 3.0, 3.1, 3.2,
3.3000000000000003, 3.4000000000000004, 3.5000000000000004,
3.6000000000000005, 3.7000000000000006, 3.8000000000000007,
3.900000000000001, 4.000000000000001, 4.100000000000001,
4.200000000000001, 4.300000000000001, 4.400000000000001,
4.500000000000002, 4.600000000000001, 4.700000000000001,
4.800000000000002, 4.900000000000002],
dtype='float64')
和
pd.Index(np.arange(0,5,0.1))
Float64Index([ 0.0, 0.1, 0.2,
0.30000000000000004, 0.4, 0.5,
0.6000000000000001, 0.7000000000000001, 0.8,
0.9, 1.0, 1.1,
1.2000000000000002, 1.3, 1.4000000000000001,
1.5, 1.6, 1.7000000000000002,
1.8, 1.9000000000000001, 2.0,
2.1, 2.2, 2.3000000000000003,
2.4000000000000004, 2.5, 2.6,
2.7, 2.8000000000000003, 2.9000000000000004,
3.0, 3.1, 3.2,
3.3000000000000003, 3.4000000000000004, 3.5,
3.6, 3.7, 3.8000000000000003,
3.9000000000000004, 4.0, 4.1000000000000005,
4.2, 4.3, 4.4,
4.5, 4.6000000000000005, 4.7,
4.800000000000001, 4.9],
dtype='float64')
和
pd.Index(np.arange(3,5,0.5))
Float64Index([3.0, 3.5, 4.0, 4.5], dtype='float64')
這肯定與Numpy:
np.arange(3,5,0.1)[5]
3.5000000000000004
和
np.arange(3,5,0.1)[5] == 3.5
False
這種情況記錄在Numpy arange檔案中:
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
輸出的長度可能在數值上不穩定。
Another stability issue is due to the internal implementation of numpy.arange. The actual step value used to populate the array is dtype(start step) - dtype(start) and not step. Precision loss can occur here, due to casting or due to using floating points when start is much larger than step. This can lead to unexpected behaviour.
It looks like np.linspace might be able to help you out here:
pd.Index(np.linspace(3,5,num=21))
Float64Index([3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2,
4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0],
dtype='float64')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427585.html
上一篇:根據另一列的條件僅替換列的某些值
