Working With Nullable Integers

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to use the nullable integer data type in pandas, which is an efficient way to handle integer data that may contain missing values. We will learn how to construct arrays with this data type, perform operations, and handle missing values effectively.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataSelectionGroup -.-> pandas/select_rows("`Select Rows`") pandas/DataSelectionGroup -.-> pandas/conditional_selection("`Conditional Selection`") pandas/DataSelectionGroup -.-> pandas/slicing("`Slicing`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") subgraph Lab Skills python/comments -.-> lab-65448{{"`Working With Nullable Integers`"}} python/with_statement -.-> lab-65448{{"`Working With Nullable Integers`"}} pandas/select_columns -.-> lab-65448{{"`Working With Nullable Integers`"}} pandas/select_rows -.-> lab-65448{{"`Working With Nullable Integers`"}} pandas/conditional_selection -.-> lab-65448{{"`Working With Nullable Integers`"}} pandas/slicing -.-> lab-65448{{"`Working With Nullable Integers`"}} python/conditional_statements -.-> lab-65448{{"`Working With Nullable Integers`"}} python/lists -.-> lab-65448{{"`Working With Nullable Integers`"}} python/tuples -.-> lab-65448{{"`Working With Nullable Integers`"}} python/importing_modules -.-> lab-65448{{"`Working With Nullable Integers`"}} python/standard_libraries -.-> lab-65448{{"`Working With Nullable Integers`"}} python/numerical_computing -.-> lab-65448{{"`Working With Nullable Integers`"}} python/data_analysis -.-> lab-65448{{"`Working With Nullable Integers`"}} end

Constructing Nullable Integer Arrays

Pandas provides the IntegerArray class for creating arrays of nullable integers. Let's start by creating an IntegerArray.

## Import necessary libraries
import pandas as pd
import numpy as np

## Create an IntegerArray with missing values
arr = pd.array([1, 2, None], dtype=pd.Int64Dtype())
## Output: <IntegerArray>
## [1, 2, <NA>]
## Length: 3, dtype: Int64

You can also use the string alias "Int64" to specify the data type when creating the array. All NA-like values are replaced with pandas.NA.

## Create an IntegerArray using the "Int64" string alias
arr = pd.array([1, 2, np.nan], dtype="Int64")
## Output: <IntegerArray>
## [1, 2, <NA>]
## Length: 3, dtype: Int64

Storing the IntegerArray in a DataFrame or Series

Once you have created an IntegerArray, you can store it in a DataFrame or Series. Let's create a Series from the IntegerArray we created.

## Create a Series from the IntegerArray
series = pd.Series(arr)

Performing Operations with Nullable Integer Arrays

You can perform various operations with nullable integer arrays, such as arithmetic operations, comparisons, and slicing.

## Create a Series with nullable integer type
s = pd.Series([1, 2, None], dtype="Int64")

## Perform arithmetic operation
s_plus_one = s + 1 ## adds 1 to each element in the series

## Perform comparison
comparison = s == 1 ## checks if each element in the series is equal to 1

## Perform slicing operation
sliced = s.iloc[1:3] ## selects the second and third elements in the series

Handling Missing Values with pandas.NA

The IntegerArray class uses pandas.NA as its scalar missing value. When you slice a single element that's missing, it will return pandas.NA.

## Create an IntegerArray with a missing value
a = pd.array([1, None], dtype="Int64")

## Slice the second element which is a missing value
missing_value = a[1]
## Output: <NA>

Summary

This lab demonstrated how to work with nullable integer data types in pandas, including how to construct arrays, store them in a DataFrame or Series, perform operations, and handle missing values. By using the nullable integer data type, you can handle integer data with missing values more efficiently.

Other Python Tutorials you may like