
- 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 to_orc() Method
The ORC (Optimized Row Columnar) format is a binary columnar serialization designed for efficient reading and writing of DataFrames, similar to the Parquet Format. It provides compact and high-speed columnar data storage, facilitating efficient data exchange across different data analysis tools.
The Pandas DataFrame.to_orc() method allows you to save DataFrames in the ORC format, enabling efficient data storage and sharing across different tools. However, there are some limitations −
Timezone handling: Timezones in datetime columns are not preserved when saving DataFrames in ORC format.
Platform support: Windows operating system is not supported for to_orc() and read_orc() as of now.
Note: The to_orc() method requires the pyarrow library (version >= 7.0.0). It is highly recommended to install the pyarrow library using Conda package manager to prevent compatibility issues. You can install it using the following command −conda install pyarrow
Syntax
Following is the syntax of the Python Pandas to_orc() method −
DataFrame.to_orc(path=None, *, engine='pyarrow', index=None, engine_kwargs=None)
Parameters
The Python Pandas DataFrame.to_orc() method accepts the below parameters −
path: Specifies the file path or file-like object where the ORC file will be saved. If None, a bytes object is returned.
engine: It specifies ORC library to use. Currently, only 'pyarrow' is supported.
index: Whether to include the DataFrame's index in the output ORC file. If set to False exclude the index. If True, Includes the index in the output file. By default it is set to None, indexes other than RangeIndex will be included as columns.
engine_kwargs: Additional keyword arguments passed to pyarrow.orc.write_table().
Return Value
The Pandas DataFrame.to_orc() method returns bytes object, if path parameter is set to None. Otherwise, returns None but saves the DataFrame as an ORC file at the specified path.
Errors
The to_orc() method raises a NotImplementedError if the DataFrame columns contains unsupported dtypes, such as category, unsigned integers, interval, period, or sparse data.
ValueError is raised if the pyarrow engine is not used.
Example: Saving a DataFrame as an ORC File
Here is a basic example demonstrating saving a Pandas DataFrame object into an ORC file format using the DataFrame.to_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") print("\nDataFrame is successfully saved as an ORC file.")
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 |
If you visit the path where the ORC files are saved, you can observe the generated orc file.
Example: Save Pandas DataFrame to In-Memory orc
This example saves a Pandas DataFrame object into a in-memory ORC file using the DataFrame.to_orc() method.
import pandas as pd import io # Create a DataFrame df = pd.DataFrame(data={"Col1": [1, 2], "Col2": [3.0, 4.0]}) 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)
While executing the above code we get the following output −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 1 | 3.0 |
1 | 2 | 4.0 |
Col_1 | Col_2 | |
---|---|---|
0 | 1 | 3.0 |
1 | 2 | 4.0 |
Example: Saving ORC file Without Index
The ORC format does not support serializing custom indexes directly. To avoid the errors, you can reset the DataFrame index before saving it as an ORC file.
import pandas as pd # Create a DataFrame with an index df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) print("Original DataFrame:") print(df) # Save the DataFrame without its index df.reset_index().to_orc("data_no_index.orc") # Read the file output = pd.read_orc("data_no_index.orc") print("\nSaved DataFrame without index:") print(output)
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
r1 | 1 | a |
r2 | 2 | b |
r3 | 3 | c |
index | Col_1 | Col_2 | |
---|---|---|---|
0 | r1 | 1 | a |
1 | r2 | 2 | b |
2 | r3 | 3 | c |