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.

pd.NA vs np.nan

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.

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 code 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:

Example 1: pd.NA vs np.nan for pandas

Now, on interpolating the dataframe, the nan value will contain some value but the NA value will work differently.

print(df.interpolate())

Output:

Example 2: pd.NA vs np.nan for pandas

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.