Forum Discussion

BharathS1307's avatar
BharathS1307
Frequent Visitor
1 year ago
Solved

Python visual showing error - single positional indexer is out of bounds

I am facing this error in Power BI after changing the datasource of the tables used in the visuals. the table which if feeded to this visuals have data but I am facing this error. 
  • Ritaf1983's avatar
    1 year ago

    Hi BharathS1307 

    This error usually happens when the Python script in Power BI tries to access a row or column that does not exist. Possible causes and solutions:

    1. No data is being passed to the Python visual.
      Even if the source table has data, Power BI filters may be removing all rows before they reach the Python script. Try adding a regular table visual with the same filters to check if any data is available.

    2. The column names or structure have changed.
      If you modified the data source, column names or order might have changed. To debug, add this line inside your script:
      print(df.head())

    3. The script is trying to access a row that does not exist.
      If the script includes df.iloc[0] but the table is empty, this will cause the error. Add a check before accessing rows:
      if not df.empty:
      first_row = df.iloc[0]
      else:
      print("No data available")

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

  • rohit1991's avatar
    1 year ago

    Hi BharathS1307 

    This error occurs when Python tries to access an index that does not exist after changing the data source. Try these fixes:

    1️⃣ Check Data Frame Shape
    Add this line at the start of your script to debug:

    print(df.shape)  # Check if the dataframe has rows
    print(df.head())  # View first few rows
    
    • If (0, X), your table is empty after transformation.

    2️⃣ Check Column Names

    • Ensure column names in the script match the new data source.
    • print(df.columns)  # Check available columns
      

    3️⃣ Handle Index Errors Safely
    Modify your script to prevent indexing errors:

    if not df.empty:
        value = df.iloc[0, 0]  # Adjust index based on data
    else:
        value = "No Data"
    

    4️⃣ Refresh Data & Check M Query

    • Manually refresh the dataset in Power BI.
    • Verify the Power Query Editor to ensure data is loading correctly.