Skip to content

BUG: assert_frame_equals fails when MultiIndex elements are None #54521

Open
@attack68

Description

@attack68

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

df = DataFrame({
    "Currency": ["USD", "USD", "USD"],
    "Collateral": [None, None, None],
    "Payment": [dt(2023, 1, 1), dt(2023, 1, 1), dt(2023, 2, 1)],
    "Cashflow": [100.0, 100.0, 200.0],
})
gf = df.groupby(["Currency", "Collateral", "Payment"], dropna=False)
       .sum()
       .unstack([0, 1])
       .droplevel(0, axis=1)

Issue Description

Screenshot 2023-08-13 at 09 02 18

This regards assert_frame_equal as part of a test suite.

The first problem with regards to testing gf is that I cannot create a MultiIndex and instantiate its dtypes directly.

expected = DataFrame(
    [200.0, 200.0],
    columns=MultiIndex.from_tuples([("USD", np.nan)], names=["Currency", "Collateral"]),
    index=[dt(2023, 1, 1), dt(2023, 2, 1)]
)
assert_frame_equal(gf, expected, atol=1e-5)

>> Traceback..
>       assert_attr_equal("dtype", left, right, obj=obj)
E       AssertionError: MultiIndex level [1] are different
E       
E       Attribute "dtype" are different
E       [left]:  float64
E       [right]: object

But I can manipulate it like this:

expected = DataFrame(
    [200.0, 200.0],
    columns=MultiIndex.from_arrays([
        Index(["USD"], name="Currency"), 
        Index([None], name="Collateral", dtype="float64")
    ]),
    index=[dt(2023, 1, 1), dt(2023, 2, 1)]
)
assert_frame_equal(gf, expected, atol=1e-5)

>> Traceback..
>       assert_frame_equal(result, expected, atol=1e-4)
E       AssertionError: DataFrame.iloc[:, 0] (column name="('USD', nan)") are different
E       
E       Attribute "name" are different
E       [left]:  ('USD', nan)
E       [right]: ('USD', nan)

There is a NA check for name but apparently it doesn't nest within the tuple so that this fails.

Expected Behavior

Should assert True. Na nest should be checked and works fine.

Installed Versions

2.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateMultiIndexTestingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions