What is the DataFrame.equals() method in Polars?
Polars is a DataFrame library implemented in Rust with bindings for Python. Due to its efficiency and scalability, Polars is particularly well-suited for data manipulation and analysis tasks.
The DataFrame.equals() method
The DataFrame.equals() method in Polars is used to check whether two DataFrames are equal. It compares the values in corresponding positions of the two DataFrames and returns a boolean indicating whether they are identical. Optionally, we can specify whether null values should be considered equal.
Syntax
DataFrame.equals(other: DataFrame,*,null_equal: bool = True,)
Parameters
Here are the parameters that DataFrame.equals() function will take:
other: DataFrame to compare with.null_equal(optional): Consider null values as equal (default is True).
It will return a boolean value.
Code
We’ll delve into the practical use of the DataFrame.equals() method in the Polars library. This method determines if two DataFrames are identical by comparing their values position-wise. We have created two DataFrames, dataframe_1 and dataframe_2, and we’ll employ the DataFrame.equals() method to examine their equality.
import polars as pl# Create two DataFramesdataframe_1 = pl.DataFrame({"id": [101, 102, 103],"price": [45.0, 55.0, 65.0],"category": ["electronics", "clothing", "home_goods"],})dataframe_2 = pl.DataFrame({"category": [103, 102, 101],"price": [65.0, 55.0, 45.0],"id": ["home_goods", "clothing", "electronics"],})# Checking if dataframe_1 equals dataframe_1result_1 = dataframe_1.equals(dataframe_1)print(f"dataframe_1 equals dataframe_1: {result_1}")# Checking if dataframe_1 equals dataframe_2, considering null valuesresult_2 = dataframe_1.equals(dataframe_2)print(f"dataframe_1 equals dataframe_2: {result_2}")
Explanation
Now, let's break down the code. We start by importing the Polars library. Then we created two DataFrames, dataframe_1 and dataframe_2, with different values. The equals method is used to compare these DataFrames:
Line 21: We check whether
dataframe_1is equal to itself. Because it is identical, the result will beTrue.Line 25: We checks whether
dataframe_1is equal todataframe_2, considering null values as equal. The result will beFalsebecause the values in corresponding positions are not the same, and null values are not considered equal by default.
Wrap up
Understanding the DataFrame.equals() method is crucial for ensuring data consistency and integrity when working with Polars DataFrames. As we explore the capabilities of the Polars library, keep in mind that this method provides a reliable means of comparing and validating the equality of DataFrames concisely and efficiently.
Free Resources