Python Forum

Full Version: Error in Python Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following python code gives an error.

%reload_ext nb_black

import yfinance
import pandas as pd
import numpy
import matplotlib.pyplot as plt

portfolio_composition = [("MSFT", 0.5), ("AAPL", 0.2), ("GOOG", 0.2)]
returns = pd.Dataframe({})

for t in portfolio_composition:
    name = t[0]
    ticker = yfinance.Ticker(name)
    data = ticker.history(interval="id")
    start = "2010-01-01", end = "2019-12-31"

    data["return_%s"(name)] = data["Close"].pct_change(1)
    returns.join(data[["returns_%s" % (name)]], how="outer").dropna()
It is the start line and the error is now shown:

Error:
File "<ipython-input-1-c1daa2af4c39>", line 15 start = "2010-01-01", end = "2019-12-31" ^ SyntaxError: can't assign to literal
I am not sure what is incorrect. At first it gave me an error on the line:
returns = pd.Dataframe({})
It did not like the dataframe. An attribute was missing.

But now it is throwing this error.

What bis wrogn with the syntax?

Any help appreciated. Thanks in advance.

Respectfully,

ErnesrtTBass
start, end = "2010-01-01", "2019-12-31"
The syntax you've used in line 15 for assigning multiple variables is not valid in Python. You'd get the same error with something like a = 2, b = 3. You can address this by assigning start and end on two separate lines, or this would also work:
start, end = "2010-01-01", "2019-12-31"
I will give it a try. Thank you for your help.

The way that I showed in the codebox, was that the old way?

I got this from legacy code.

Respectfully,

ErnestTBass
(Jun-04-2020, 03:54 PM)ErnestTBass Wrote: [ -> ]The way that I showed in the codebox, was that the old way?
No, it was never a valid code. Probably you have typo - if it was semicolon instead of comma, it will be valid though unpythonic code