我在 Python 3.10.3 上使用
pandas==1.4.1
和 dython==0.7.1
运行此测试。我从日期数据创建一个简单的相关数据框,并将其与我期望得到的数据进行比较。尽管如此,根据 Pandas 的说法,尽管 DF 具有相同的数据和相同的 dtypes
,但它们并不相等。我错过了什么?
import pandas as pd
from datetime import datetime, timedelta
from dython.nominal import associations
def test_datetime_data():
dt = datetime(2020, 12, 1)
end = datetime(2020, 12, 2)
step = timedelta(seconds=5)
result = []
while dt < end:
result.append(dt.strftime('%Y-%m-%d %H:%M:%S'))
dt += step
nums = list(range(len(result)))
df = pd.DataFrame({'dates':result, 'up': nums, 'down': sorted(nums, reverse=True)})
df['dates'] = pd.to_datetime(df['dates'], format="%Y-%m-%d %H:%M:%S") # without this, this column is considered as object rather than dates
correct_corr = pd.DataFrame(columns=['dates', 'up', 'down'], index=['dates', 'up', 'down'],
data=[[1.0, 1.0, -1.0],
[1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0]])
corr = associations(df, plot=False)['corr']
assert corr.equals(correct_corr), f'datetime associations are incorrect. Test should have returned an empty dataframe, received: {corr.head()}'
corr
和correct_corr
都是[[1.0, 1.0, -1.0], [1.0, 1.0, -1.0], [-1.0, -1.0, 1.0]]
和float64
,但测试仍然失败..
您可能需要检查数据框中的数据类型以确保它们相同。
corr.info()
correct_corr.info()