Table of Contents

Introduction

The Condition Framework provides an object-oriented approach to creating and evaluating conditions within pandas DataFrames. It allows users to construct complex query-like conditions using a simple API and can easily be serialized to and from JSON for configuration purposes.

A condition object represents a logical test that can be evaluated on a DataFrame.

  • parse(df) : The method to override in subclasses to implement condition logic.
  • to_json() : Serializes the condition into a JSON-friendly format.
  • from_json(json_data) : Static method to deserialize JSON data back into a condition object.

And (example)

Represents a logical AND condition combining multiple subconditions.

  • __init__(*args) : Initializes a new AND condition with a variable number of subconditions.
  • parse(df) : Overrides the base method to evaluate the AND condition across the combined subconditions.
  • to_json() : Serializes the AND condition to JSON.
  • from_json(json_data) : Static method to deserialize an AND condition from JSON data.

Usage Examples

To create a condition combining equality and inequality checks:

condition = And(
    Equal(Column("a"), Value(10)), NotEqual(Column("b"), Value(20))
)
result = condition.parse(df)

To serialize a condition to JSON:

condition_json = condition.to_json()

To deserialize a condition from JSON:

restored_condition = Condition.from_json(condition_json)

Extending the Framework

To add new condition types to the framework, create a new subclass of Condition and implement the required methods.

Conclusion

This framework simplifies the definition and manipulation of logical conditions in pandas DataFrames and enables the conditions to be easily shared as JSON configurations.