
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Pickling
Pickling in Python, also known as serialization, is the process of converting a Python object into a byte stream, which can be saved and later deserialized back into the original object structure. In the context of Pandas, pickling enables the efficient saving and loading of DataFrames and Series objects.
The Python Pandas library provides easy to use functions for pickling DataFrames and Series objects using its to_pickle() and read_pickle() methods. These methods use Python's cPickle module, which implements a binary format for efficiently saving data structures to disk and loading them back to the Pandas object using the pickle format.
In this tutorial, we will learn about how to use Pandas built-in pickling functionalities for efficient serialization and deserialization of Pandas objects with more customization options.
Saving a Pandas Object to a Pickle File
To pickle the Pandas objects such as DataFrame, or Series, you can use the to_pickle() method, which saves them to a file in pickle format.
Example: Saving a DataFrame to a Pickle File
Following is the example that uses the to_pickle() method for saving a Pandas DataFrame object into a pickle file.
import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ "A": [1, 2, 3], "B": [4, 5, 6] }) # Display the Input DataFrame print("Original DataFrame:") print(df) # Save the DataFrame to a pickle file df.to_pickle("dataframe.pkl") print('\nDataFrame is successfully saved as a pickle file named "dataframe.pkl".')
When we run above program, it produces following result −
Original DataFrame:
A | B | |
---|---|---|
0 | 0 | 4 |
1 | 1 | 5 |
2 | 2 | 6 |
If you visit the folder where the pickle files are saved, you can observe the generated pickle file.
Example: Saving a Series to a Pickle File
Following is the example that uses the to_pickle() method for saving a Pandas Series object into a pickle file.
import pandas as pd # Creating a Pandas Series s = pd.Series([1, 2, 3, 4]) # Display the Input Series print("Original Series:") print(s) # Save the Series as a pickle file s.to_pickle("series_to_pickle_file.pkl") print('\nSeries is successfully saved as a pickle file named "series_to_pickle_file.pkl".')
When we run above program, it produces following result −
Original Series: 0 1 1 2 2 3 3 4 dtype: int64 Series is successfully saved as a pickle file named "series_to_pickle_file.pkl".
In this example, the Series object is saved to a file named "series_to_pickle_file.pkl" in the current directory.
Loading a Pickled Pandas Object
For loading a pickled file into the Pandas object (Series, or DataFrame objects), you can use the read_pickle() method from Pandas. This will deserialize the byte stream and recreate the Pandas object.
It's important to note that, loading pickled data from untrusted sources can be risky. Always verify the source before deserializing a pickle file.
Example
This example loads a Pandas Series object from a pickle file using the Pandas read_pickle() method.
import pandas as pd # Creating a Pandas Series s = pd.Series([1, 2, 3, 4], index=["cat", "dog", "fish", "mouse"]) # Display the Input Series print("Original Series:") print(s) # Save the Series as a pickle file s.to_pickle("series_read_pickle_file.pkl") # Load the Series from the pickle file unpickled_series = pd.read_pickle("series_read_pickle_file.pkl") print("\nLoaded Series:") print(unpickled_series)
While executing the above code we get the following output −
Original Series: cat 1 dog 2 fish 3 mouse 4 dtype: int64 Loaded Series: cat 1 dog 2 fish 3 mouse 4 dtype: int64
Working with Compressed Pickle Files
Pandas pickling functionality also supports reading and writing compressed pickle files. The following compression formats are supported:
gzip
bz2
xz
zstd
You can specify the compression format either explicitly or by inferring it from the file extension. To do this, set the compression parameter of the to_pickle() and read_pickle() methods to 'infer'.
Example: Saving a Compressed Pickle File
Here is an example of demonstrating how to save a DataFrame to a compressed pickle file using gzip compression.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)}) print("Original DataFrame:") print(df) # Save the DataFrame to a pickle file with gzip compression df.to_pickle("dataframe_compressed.pkl.gz", compression="gzip") print("\nDataFrame is successfully saved as a pickle file with gzip compression.")
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
Example: Loading a Compressed Pickle File
The following example demonstrates how to use the read_pickle() method to read the gzip compressed pickle file into a Pandas object. To load a compressed pickle file, you simply need to specify the compression type.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)}) print("Original DataFrame:") print(df) # Save the DataFrame to a pickle file with gzip compression df.to_pickle("dataframe_compressed.pkl.gz", compression="gzip") # Load the compressed file compressed_df = pd.read_pickle("dataframe_compressed.pkl.gz") print("\nLoaded Compressed DataFrame:") print(compressed_df)
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
Inferring Compression Type from a Pickle File Extension
PaPandas will automatically detect the compression type if the compression parameter is set to infer and the file ends with .gz, .bz2, .xz, or .zst extensions.
Example
This example sets the compression parameter value to infer for automatically inferring the compression type from the file extension.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": [1, 2, 3, 4, 5], "Col_2": ["a", "b", "c", "d", "e"]}) print("Original DataFrame:") print(df) # Save with inferred compression type df.to_pickle("dataframe.pkl.xz", compression="infer") # Load with inferred compression type df_loaded_inferred = pd.read_pickle("dataframe.pkl.xz", compression="infer") print("\nLoaded with inferred compression type:") print(df_loaded_inferred)
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |
3 | 4 | d |
4 | 5 | e |
Col_1 | Col_2 | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |
3 | 4 | d |
4 | 5 | e |