Skip to content

Commit 490372f

Browse files
Copilotmmcky
andcommitted
Fix TypeError in Polars exercise and update pivot API usage
Co-authored-by: mmcky <[email protected]>
1 parent de01b78 commit 490372f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lectures/polars.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def read_data(ticker_list,
690690
ticker_df = pl.concat(all_data)
691691
692692
# Pivot to have tickers as columns
693-
ticker_df = ticker_df.pivot(values='Close', index='Date', columns='ticker')
693+
ticker_df = ticker_df.pivot(values='Close', index='Date', on='ticker')
694694
695695
return ticker_df
696696
@@ -721,12 +721,12 @@ First, you can extract the data and perform the calculation such as:
721721
first_prices = ticker[0] # First row
722722
last_prices = ticker[-1] # Last row
723723
724-
# Convert to pandas for easier calculation
725-
first_pd = ticker.head(1).to_pandas().iloc[0]
726-
last_pd = ticker.tail(1).to_pandas().iloc[0]
724+
# Convert to pandas for easier calculation, excluding Date column to avoid type errors
725+
numeric_cols = [col for col in ticker.columns if col != 'Date']
726+
first_pd = ticker.head(1).select(numeric_cols).to_pandas().iloc[0]
727+
last_pd = ticker.tail(1).select(numeric_cols).to_pandas().iloc[0]
727728
728729
price_change = (last_pd - first_pd) / first_pd * 100
729-
price_change = price_change.dropna() # Remove Date column
730730
price_change
731731
```
732732

0 commit comments

Comments
 (0)