# How does the pandas series.equals() method handle the null values?

PandasServer Side ProgrammingProgramming

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Pandas with Python

24 Lectures 6 hours

#### Basics Data Science with Numpy, Pandas and Matplotlib

10 Lectures 2.5 hours

It is very common to have missing values in a series object, and if you want to compare that type of series objects then the ordinary comparison does not work because nan != nan, In that case, we can use the equals() method. The equals() method considers Nan’s in the same location to be equal.

The fundamental operation of the pandas series.equals() method is used to compare two series for equality. it returns True if the two series have the same elements and shape, and returns False if the two series are unequal.

## Example 1

In the following example, two series objects series1 and series2 are applied to the equals() method to compare equality.

# importing pandas package
import pandas as pd
import numpy as np

# create pandas Series1
series1 = pd.Series([67, 18, np.nan, 50, 39])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series([67, 18, np.nan, 50, 39])
print("second series object:",series2)

result = series1.equals(series2)

print("Result:", result)

## Explanation

Since the series objects are exactly similar and there is a nan value present in two series objects at the same location.

## Output

The output is mentioned below −

First series object:
0    67.0
1    18.0
2     NaN
3    50.0
4    39.0
dtype: float64

Second series object:
0    67.0
1    18.0
2     NaN
3    50.0
4    39.0
dtype: float64

Result: True

Here the two series objects are exactly similar with the same data type, and the equals() method returns True as a result. Also, you can see that NaNs are considered equal if they occur at the same location.

## Example 2

In the following example, two series objects series1 and series2 are verified for equality by applying the equals() method.

import pandas as pd
import numpy as np

# create pandas Series1
series1 = pd.Series([92, 68, 65, np.nan])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series(['92', '68', '65', np.nan])
print("second series object:",series2)

result = series1.equals(series2)
print("Result :", result)

## Explanation

Here the series objects are similar but the data types of the elements are different.

## Output

The output is given below −

First series object:
0    92.0
1    68.0
2    65.0
3     NaN
dtype: float64

Second series object:
0     92
1     68
2     65
3    NaN
dtype: object

Result: False

For the above given example, the equality function equals() returns False. Because the data types of the elements in the two series objects are not the same.