Home »
Python »
Python Programs
pd.NA vs np.nan for pandas
Here, we are going to learn about the pd.NA vs np.nan for pandas.
Submitted by Pranit Sharma, on November 15, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python.
Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.
pd.NA vs np.nan
The only difference between pandas NA and NumPy nan is that NA is still an experimental feature that it can still change without a warning and, also as compared to Numpy nan which behaves with every operation, pandas NA works differently in certain operations.
Let us understand both concepts with the help of an example,
Python program to demonstrate the pd.NA vs np.nan for pandas
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a dictionary
d = {"a": [0, pd.NA, 2], "b": [0, np.nan, 2]}
# Creating DataFrame
df = pd.DataFrame(d)
# Display dataframe
print('Original DataFrame:\n',df,'\n')
Output
The output of the above program is:
Now, on interpolating the dataframe, the nan value will contain some value but the NA value will work differently.
print(df.interpolate())
Output
The output of the above program is:
Python Pandas Programs »