Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in Python Code
#1
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
Reply
#2
start, end = "2010-01-01", "2019-12-31"
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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"
Reply
#4
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
Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,601 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,451 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,469 Mar-27-2023, 07:38 AM
Last Post: buran
  Error in if-then-else python code Led_Zeppelin 6 2,286 Jul-27-2022, 07:53 PM
Last Post: deanhystad
  Facing error while executing below Python code ramu4651 1 5,632 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  Skeleton file export error Python Code pepapoha 4 3,426 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,143 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Syntax error in python code sample ErnestTBass 5 3,059 Aug-14-2020, 07:14 PM
Last Post: deanhystad
  Python Code error Riteshfrancis 2 2,397 May-21-2020, 06:37 AM
Last Post: Riteshfrancis
  Simple python code error cls0724 5 3,270 Mar-12-2020, 07:45 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020