Python Pandas read_feather() Method



The read_feather() method in Python's Pandas library allows you to read/load the feather-format object from the file path into a Pandas object, enabling fast and efficient data retrieval.

The Feather file format is a portable file format for saving and retrieving the DataFrame. It is a fast and language-independent binary file format designed for efficient data interchange between data analysis languages. It is supported by both Python and R languages, ensuring easy data sharing, fast reading and writing capabilities with less memory usage.

Before using the read_feather() method, please ensure that the 'pyarrow' library is installed, as it is an optional dependency. We need to install it by using the following command −

pip install pyarrow

Syntax

Following is the syntax of the Python Pandas read_feather() method −

pandas.read_feather(path, columns=None, use_threads=True, storage_options=None, dtype_backend=<no_default>)

Parameters

The Python Pandas read_feather() method accepts the below parameters −

  • path: This method accepts a string, path object, or file-like object that specifies the file path. It can be a local file path or a remote URL supporting schemes like http, ftp, s3, and file. For local files, the expected format is file://localhost/path/to/file.feather.

  • columns: Specifies which columns to read. If not provided, all columns are read.

  • use_threads: Specifies whether to parallelize reading using multiple threads.

  • storage_options: Additional options for storage connections, such as host, port, username, password, etc.

  • dtype_backend: Specifies the data type backend for the resulting DataFrame.

Return Value

The Pandas read_feather() method returns the same type of Pandas object that was stored in the feather format file.

Example: Loading a Feather File into Pandas Object

Here is a basic example demonstrating loading a Pandas DataFrame object from a feather file using the Pandas read_feather() method.

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 as a feather file
df.to_feather("df_feather_file.feather")

# Load the DataFrame from the feather file
result = pd.read_feather("df_feather_file.feather")
print("DataFrame Loaded from Feather File:")
print(result)

When we run above program, it produces following result −

Original DataFrame:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9
DataFrame Loaded from Feather File:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9

Example: Reading Selected Columns from a Feather File

This example loads the Pandas DataFrame with the selected columns from a feather file using the Pandas read_feather() method.

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 as a feather file
df.to_feather("df_feather_file.feather")

# Read only specific columns
df = pd.read_feather('df_feather_file.feather', columns=['Col_1'])
print("DataFrame with Selected Column:")
print(df)

While executing the above code we get the following output −

Original DataFrame:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9
DataFrame with Selected Column:
Col_1
0 0
1 1
2 2
3 3
4 4

Example: Loading a feather File with Compression

The following example initially saves a compressed feather file and then loads it using the read_feather() method to read the compressed feather file back into the Pandas object.

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 feather file with compression
df.to_feather('compressed_data.feather', compression='zstd')
print("DataFrame saved with compression..")

# Load the compressed file
compressed_df = pd.read_feather("compressed_data.feather")
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
DataFrame saved with compression.. Loaded Compressed DataFrame:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9

Example: Reading from an In-Memory Feather File

The read_feather() method can also read the In-Memory feather file. Here is an example demonstrates the same.

import pandas as pd
import io

# Create a Pandas DataFrame 
df = pd.DataFrame(data={'Col_1': [1, 2], 'Col_2': [3.0, 4.0]})

# Display the Input DataFrame
print("Original DataFrame:")
print(df)

# Save the DataFrame as In-Memory feather
buf = io.BytesIO()
df.to_feather(buf)

# Read the DataFrame from the in-memory buffer
loaded_df = pd.read_feather(buf)
print("DataFrame Loaded from In-Memory Feather:")
print(loaded_df)

Following is an output of the above code −

Original DataFrame:
Col_1 Col_2
0 1 3.0
1 2 4.0
DataFrame Loaded from In-Memory Feather:
Col_1 Col_2
0 1 3.0
1 2 4.0
python_pandas_io_tool.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy