Pandas的 Dataframe 不一样,尽管它们是

lf5gs5x2  于 2022-09-21  发布在  其他
关注(0)|答案(1)|浏览(139)

我在Python3.10.3上使用pandas==1.4.1dython==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()}'

corrcorrect_corr都是[[1.0, 1.0, -1.0], [1.0, 1.0, -1.0], [-1.0, -1.0, 1.0]]作为float64,但测试仍然失败。

ozxc1zmp

ozxc1zmp1#

您可能希望检查 Dataframe 中的数据类型,以确保它们是相同的。

corr.info()
correct_corr.info()

相关问题