dosubot[bot] commented on issue #30066:
URL: https://github.com/apache/superset/issues/30066#issuecomment-2320843346

   <!-- Answer -->
   I found a similar open issue that might be relevant to your problem: 
[`Error: Referenced columns not available in DataFrame` in Mixed Charts when 
one query has only nulls](https://github.com/apache/superset/issues/29933). 
This issue was last updated on August 13, 2024.
   
   To resolve the "Referenced columns not available in DataFrame" error when 
using Jinja templating for filters in a Big number with trendline chart in 
Superset version 4.0.2, you need to ensure that the columns referenced in your 
Jinja template are actually present in the DataFrame being queried. Here are 
some steps to troubleshoot and resolve the issue:
   
   1. **Verify Column Names**: Ensure that the column names used in your Jinja 
template match exactly with the column names in your DataFrame.
   
   2. **Check Query Result**: Make sure that the columns you are referencing in 
the Jinja template are included in the query result. You can do this by 
printing or logging the DataFrame columns before applying the template.
   
   3. **Update Jinja Template**: Modify your Jinja template to correctly 
reference the available columns.
   
   Additionally, Superset provides several Jinja context functions that can be 
used to dynamically access and manipulate data. These functions include 
`url_param`, `current_user_id`, `current_username`, `current_user_email`, 
`filter_values`, and `get_filters`. These functions can be used to ensure that 
your template references the correct columns and values.
   
   Here is an example of how you can modify your Jinja template to ensure it 
references the correct columns:
   
   ```py
   from jinja2 import Template
   
   # Example DataFrame
   data = {
       'column1': [1, 2, 3],
       'column2': [4, 5, 6]
   }
   df = pd.DataFrame(data)
   
   # Jinja template
   template_str = """
   {% if 'column1' in df.columns %}
       {{ df['column1'].sum() }}
   {% else %}
       Column 'column1' not found in DataFrame
   {% endif %}
   """
   
   # Create a Jinja template
   template = Template(template_str)
   
   # Render the template with the DataFrame
   result = template.render(df=df)
   print(result)
   ```
   
   In this example, the Jinja template checks if 'column1' is present in the 
DataFrame columns before attempting to sum its values. This prevents the error 
by ensuring that the column exists 
<sup>[[1]](https://github.com/apache/superset/blob/master/superset/connectors/sqla/models.py)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset/jinja_context.py)</sup>.
   
   
   <!-- Dosu Comment Footer -->
   *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to