pandas
airflow
data-pipeline
data-engineering
python

How to pass pandas dataframe to airflow tasks

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Apache Airflow is a powerful platform used to programmatically author, schedule, and monitor workflows. It offers rich functionality to manage workflows and comes with various operators, sensors, and hooks to execute tasks within workflows. Pandas DataFrame, a prominent data structure in Python, is extensively used for data manipulation and analysis in many data processing pipelines. In many scenarios, you may need to pass a Pandas DataFrame between Airflow tasks to maintain consistency in data processing and transformations. This article provides a detailed explanation of how to pass a Pandas DataFrame between tasks in an Apache Airflow DAG.

Storing DataFrames for Later Retrieval

Due to the distributed nature of Airflow, tasks run independently on worker nodes, which means data can't be passed directly in memory between tasks. Instead, data should be serialized and stored in a medium accessible by both tasks. Here are some common strategies:

  1. Using XComs:
    • Serialization: Pandas DataFrames can be serialized using `pickle` or `json`.
    • Size Limitation: XComs have a size limitation since they store the data in the Airflow metadata database.
  2. External Storage Systems:
    • File Storage: Save the DataFrame to a file using formats like CSV, Parquet, or Feather stored in a shared file system or cloud storage like Amazon S3.
    • Databases: Store DataFrame in a database (e.g., PostgreSQL, MySQL) that can be accessed by other tasks.
  3. In-Memory Databases:
    • Use Redis or Memcached to temporarily store the DataFrame.

Let's explore these methods with some code examples and explanations.

Using XComs to Pass DataFrames

XComs are the preferred approach for lightweight, small-size data sharing between tasks in Airflow. However, it's crucial to ensure the data stays within the size constraints.

  • Security: When using external storage systems, ensure that the data flow is secure. Use encryption techniques and access controls.
  • Performance: Data serialization and network latency can impact performance, especially with large DataFrames.
  • Cleaning Up: Implement proper cleanup of temporary files and data artifacts to prevent resource leakage and redundant data storage.
  • Error Handling: Use robust error handling to manage scenarios where data may not be available or accessible.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.