[英]dtype Int64 doesn't return view of underlying data?
我有兩個大小為(5, 5)
的數據幀,一個是 dtype int64
另一個是pd.Int64Dtype
類型。
np.random.seed(2021)
data = np.arange(25).reshape((5, 5))
one = pd.DataFrame(data, dtype='int64')
two = pd.DataFrame(data.copy(), dtype='Int64') # Notice the capital 'I'
r, c = np.random.randint(0, 5, (2, 5))
當我嘗試更改基礎數據時出現問題。
one.to_numpy()[r, c] = 99 # Changes the underlying data
print(one)
0 1 2 3 4
0 0 99 2 3 4
1 5 6 7 8 99
2 10 11 12 13 14
3 15 99 17 18 19
4 99 99 22 23 24
two.to_numpy()[r, c] = 99 # Doesn't change the underlying data
print(two)
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
我知道DataFrame.to_numpy
不一定返回視圖。
副本:bool,默認為 False
是否確保返回值不是另一個數組的視圖。 請注意, copy=False 不能確保 to_numpy() 是 no-copy 。 相反,copy=True 確保制作副本,即使不是絕對必要的。
如何以矢量化方式更改 DataFrame 中的給定位置( r
, c
)? 我有一個使用 for loop + .iloc
的解決方案。 對於它的價值,我的熊貓版本是1.3.1
。
數據類型為Int64
的 ExtensionBlocks 不支持 numpy 分配是正確的,因為它們被視為 5 個獨立的塊而不是單個數字塊。 這會影響生成對底層結構的統一可修改引用的能力。
您可以通過從管理器訪問塊來觀察這一點(注意這只是為了觀察目的):
print('One Blocks')
for blk in one._mgr.blocks:
print(blk)
print('Two Blocks')
for blk in two._mgr.blocks:
print(blk)
Output:
One Blocks
NumericBlock: slice(0, 5, 1), 5 x 5, dtype: int64
Two Blocks
ExtensionBlock: slice(0, 1, 1), 1 x 5, dtype: Int64
ExtensionBlock: slice(1, 2, 1), 1 x 5, dtype: Int64
ExtensionBlock: slice(2, 3, 1), 1 x 5, dtype: Int64
ExtensionBlock: slice(3, 4, 1), 1 x 5, dtype: Int64
ExtensionBlock: slice(4, 5, 1), 1 x 5, dtype: Int64
請注意,DataFrame( two
)將這些作為單獨的底層結構,這意味着轉換為數組調用_interleave ,如注釋所示“底層數據在 _interleave 中復制”。
請注意,對於包含多個塊的所有數據幀都是如此。
意思很簡單:
df = pd.DataFrame({'A': [1, 2], 'B': ['a', 'b']})
df.to_numpy()[0, 0] = 5 # No Change
print(df)
A B
0 1 a
1 2 b
也不能這樣修改。
*供參考的塊
# df._mgr.blocks
NumericBlock: slice(0, 1, 1), 1 x 2, dtype: int64
ObjectBlock: slice(1, 2, 1), 1 x 2, dtype: object
考慮到這一點,我們必須使用to_numpy
生成的副本並重建 DataFrame:
a = two.to_numpy() # Store New Array
a[r, c] = 99 # Update The Values
# Reconstruct the DataFrame
two = pd.DataFrame(a, index=two.index, columns=two.columns, dtype='Int64')
astype
也可以與已知的數據類型一起使用,以確保列 map 為適當的數據類型(這可能對多個數據類型的實例有幫助):
two = pd.DataFrame(a, index=two.index, columns=two.columns).astype(two.dtypes)
Output:
print(two)
0 1 2 3 4
0 0 99 2 3 4
1 5 6 7 8 99
2 10 11 12 13 14
3 15 99 17 18 19
4 99 99 22 23 24
print(two.dtypes)
0 Int64
1 Int64
2 Int64
3 Int64
4 Int64
dtype: object
然而,考慮到這種單一替換,使用 numpy 構建 2D 掩碼可能是更好的方法:
# Build Boolean Mask (default False)
result = np.zeros(two.shape, dtype='bool')
result[r, c] = True # Set True Locations
two = two.mask(result, 99) # DataFrame.mask to replace values
或帶有DataFrame.where
的反掩碼:
# Build Boolean Mask (default True)
result = np.ones(two.shape, dtype='bool')
result[r, c] = False # Set False Locations
two = two.where(result, 99) # DataFrame.where to replace values
兩者都產生:
print(two)
0 1 2 3 4
0 0 99 2 3 4
1 5 6 7 8 99
2 10 11 12 13 14
3 15 99 17 18 19
4 99 99 22 23 24
print(two.dtypes)
0 Int64
1 Int64
2 Int64
3 Int64
4 Int64
dtype: object
*這些方法的好處是不會丟失 dtype 信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.