
- 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 read_orc() Method
The read_orc() method in Pandas library allows you to load data stored in the ORC format into a Pandas DataFrame. ORC (Optimized Row Columnar) is a binary columnar storage format designed for efficient data analysis, making reading and writing DataFrames highly efficient. It offers support for smallest, fastest columnar data storage and sharing across data analysis languages and this format is similar to the Parquet Format.
The read_orc() method supports reading ORC files from various storage back-ends, including local files, URLs, and cloud storage services.
Here are some key points to consider when using the read_orc() method −
Installation Requirements: This format requires the pyarrow library for both reading and writing ORC files in Pandas.
Timezone handling: Timezones in datetime columns are not preserved when saving DataFrames in ORC format.
Platform support: This method is not supported on Windows operating system as of now.
Note: Before using the read_orc() method, you need to install the 'pyarrow' library and it is highly recommended to install it using the conda installer to avoid compatibility issues −conda install pyarrow
In this tutorial, we will explore the read_orc() method from the Pandas library and how to use it for reading data stored in the ORC format.
Syntax
Following is the syntax of the Python Pandas read_orc() method −
pandas.read_orc(path, columns=None, dtype_backend=<no_default>, filesystem=None, **kwargs)
Parameters
The Python Pandas read_orc() method accepts the below parameters −
path: This method accepts a string, path object, or file-like object that specifies the ORC 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/table.orc.
columns: Specifies which columns to load from the ORC file. If not specified, all columns are loaded.
dtype_backend: Defines the back-end data type applied to the resultant DataFrame ('numpy_nullable' or 'pyarrow').
filesystem: A pyarrow or fsspec filesystem object for reading the parquet file. By default None.
**kwargs: Additional arguments passed to PyArrow engine.
Return Value
The Pandas read_orc() method returns a Pandas DataFrame containing the data from the ORC file.
Example: Reading an ORC File
Here is a basic example demonstrating loading a Pandas DataFrame object from an ORC file using the Pandas read_orc() 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 an ORC file df.to_orc("df_orc_file.orc") # Load the DataFrame from the ORC file result = pd.read_orc("df_orc_file.orc") print("\nDataFrame loaded from ORC 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 |
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
Example: Reading Selected Columns from an ORC File
This example reads the Pandas DataFrame with the selected columns from an ORC file using the Pandas read_orc() 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 an ORC file df.to_orc("df_orc_file.orc") # Read only specific columns df = pd.read_orc('df_orc_file.orc', columns=['Col_1']) print("\nDataFrame from ORC file 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 |
Col_1 | |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
Example: Reading an In-Memory ORC File
This example demonstrates how to save a DataFrame as an ORC file in memory using an in-memory buffer and read it back into a DataFrame.
import pandas as pd import io # Create a DataFrame df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)}) print("Original DataFrame:") print(df) # Save the DataFrame to an in-memory buffer buffer = io.BytesIO() df.to_orc(buffer) # Read the ORC file from the buffer output = pd.read_orc(buffer) print("\nDataFrame saved as an in-memory ORC file:") print(output)
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 |