Skip to content

Series .equals does not ignore NaNs #8797

@rockg

Description

@rockg

This is a datatype thing I believe. When I construct a DataFrame and then compare rows of that, NaNs are not ignored contrary to the documentation.

import pandas as pd
import numpy as np
a = pd.DataFrame([{'A': 1, 'B': 'b', 'C': np.NAN}, {'A': 1, 'B': 'b', 'C': 1}])
b = pd.DataFrame([{'A': 1, 'B': 'b', 'C': np.NAN}, {'A': 1, 'B': 'b', 'C': 1}])

a.loc[0]
A      1
B      b
C    NaN
Name: 0, dtype: object
aa = a.loc[0]
bb = b.loc[0]
aa.equals(bb)
False

# which essentially does this in Block
aa.values == bb.values
Out[35]: array([ True,  True, False], dtype=bool)

Constructing the series outright works as advertised.

a = pd.Series({'A': 1, 'B': 'b', 'C': np.NAN})
b = pd.Series({'A': 1, 'B': 'b', 'C': np.NAN})
a.equals(b)
True
a.values == b.values
Out[30]: array([ True,  True,  True], dtype=bool)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions