Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lumibot/entities/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def get_last_price(self) -> Union[float, Decimal, None]:
float, Decimal or None

"""
return self.df["close"][-1]
return self.df["close"].iloc[-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: .iloc breaks Polars compatibility.

The self.df property returns a Polars DataFrame when _return_polars=True (see line 340). Polars Series do not have the .iloc accessor—this will raise AttributeError for users who enabled return_polars=True.

🐛 Proposed fix to handle both Pandas and Polars
     def get_last_price(self) -> Union[float, Decimal, None]:
         """Return the last price of the last bar
 
         Parameters
         ----------
         None
 
         Returns
         -------
         float, Decimal or None
 
         """
-        return self.df["close"].iloc[-1]
+        close_col = self.df["close"]
+        if isinstance(self._df, pl.DataFrame):
+            return close_col[-1]
+        return close_col.iloc[-1]
 
     def get_last_dividend(self):
         """Return the last dividend of the last bar
 
         Parameters
         ----------
         None
 
         Returns
         -------
         float
         """
         if "dividend" in self.df.columns:
-            return self.df["dividend"].iloc[-1]
+            div_col = self.df["dividend"]
+            if isinstance(self._df, pl.DataFrame):
+                return div_col[-1]
+            return div_col.iloc[-1]
         else:
             logger.debug("Unable to find 'dividend' column in bars")
             return 0

Alternatively, consistently use .pandas_df in these methods if Polars users are expected to call convenience methods:

-        return self.df["close"].iloc[-1]
+        return self.pandas_df["close"].iloc[-1]

This leverages the existing caching in pandas_df property but note it may trigger a conversion warning for Polars users.

Also applies to: 571-572

🤖 Prompt for AI Agents
In `@lumibot/entities/bars.py` at line 558, Replace direct use of .iloc on self.df
(which can be a Polars DataFrame) with the cached pandas conversion: use
self.pandas_df["close"].iloc[-1] instead of self.df["close"].iloc[-1], and make
the same change for the other occurrences noted (the two uses around lines
571-572). This keeps behavior consistent for both Polars and Pandas by
leveraging the existing pandas_df property and its caching.


def get_last_dividend(self):
"""Return the last dividend of the last bar
Expand All @@ -569,7 +569,7 @@ def get_last_dividend(self):
float
"""
if "dividend" in self.df.columns:
return self.df["dividend"][-1]
return self.df["dividend"].iloc[-1]
else:
logger.debug("Unable to find 'dividend' column in bars")
return 0
Expand Down