how to convert {{ds}} airflow macro to DATE datatype

Viewed 763
df['date'] = {{ds}}

in this data frame I want a value of type DATE to store, but all I get is a string datatype.

1 Answers

You can't use macros outside of operators scope. Macros are rendering as part of operator execution otherwise it's just a plain text string. {{ ds }} can work only on templated fields of the operator. In your example it's clear that you are looking to get the value of ds inside a python callable rather than in templated fields thus you can get the value as:

def func(**kwargs):
    execution_date = kwargs['execution_date']
    df = pd.DataFrame()
    df['execution_date'] = execution_date
    #If you want also to convert the column to datetime you can add
    df['execution_date'] = pd.to_datetime(df['execution_date'])
    print(df)

op = PythonOperator(
    task_id='example',
    python_callable=func,
    provide_context=True,
    dag=dag
)
Related