首次進口:散列熊貓據幀中斷
import pandas as pd
import numpy as np
import hashlib
接下來,考慮以下因素:
np.random.seed(42)
arr = np.random.choice([41, 43, 42], size=(3,3))
df = pd.DataFrame(arr)
print(arr)
print(df)
print(hashlib.sha256(arr.tobytes()).hexdigest())
print(hashlib.sha256(df.values.tobytes()).hexdigest())
這個片段的多次執行產生相同的哈希值的兩倍所有的時間:ddfee4572d380bef86d3ebe3cb7bfa7c68b7744f55f67f4e1ca5f6872c2c9ba1
。
然而,如果我們考慮以下因素:
np.random.seed(42)
arr = np.random.choice(['foo', 'bar', 42], size=(3,3))
df = pd.DataFrame(arr)
print(arr)
print(df)
print(hashlib.sha256(arr.tobytes()).hexdigest())
print(hashlib.sha256(df.values.tobytes()).hexdigest())
注有在現在的數據字符串。對於不同的評估,arr
的散列值是固定的(52db9328682317c44370b8186a5c6bae75f2a94c9d0d5b24d61f602857acd3de
),但每次更改pandas.DataFrame
。
圍繞它的任何pythonic方式?沒有Pythonic?
編輯:相關鏈接:
[博客文章概要](http://drorata.github.io/posts/2017/May/26/when-trying-to-hash-a-data-frame/ ) – Dror