我在Python3.10.3上使用pandas==1.4.1
和dython==0.7.1
运行此测试。我从日期数据创建了一个简单的关联 Dataframe ,并将其与我期望得到的结果进行比较。尽管如此,根据Pandas的说法,DFS并不相等,即使它们拥有相同的数据和相同的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
,但测试仍然失败。
1条答案
按热度按时间ozxc1zmp1#
您可能希望检查 Dataframe 中的数据类型,以确保它们是相同的。