ZacAttack ZacAttack - 2 months ago 8
Python Question

SyntaxError: invalid syntax Python 3.5 when trying to print in python, even with a simple print("Hello World")

I just finished deleting a bunch of files off my desktop and other misc. folders. I just re-opened my project that I have been working on and now it won't print, even with a simple 'Hello World' line 144.

I have opened a new python project and I just printed("hello world") and it works fine, so I tried coping my code to that same sheet and saving it as a different name and I get the same error.

This is the code I am using below; print is all the way at the bottom.

from pandas_datareader import data as dreader
import pandas as pd
from datetime import datetime
import dateutil.parser
from tkinter import *


# Sets the max rows that can be displayed
# when the program is executed
pd.options.display.max_rows = 200



# df is the name of the dataframe, it is
# reading the csv file containing data loaded
# from yahoo finance(Date,Open,High,Low,Close
# volume,adj close,)the name of the ticker
# is placed before _data.csv i.e. the ticker aapl
# would have a csv file named aapl_data.csv.
df = pd.read_csv("cde_data.csv")


# resets the index back to the pandas default
# i.e. index starts at 0 for the first row and
# 1 for the second and continues by one till the
# end of the data in the above csv file.
df.reset_index()



# the following code will allow for filtering of the datafram
# based on the year, day of week (dow), and month. It then gets
# applied to the dataframe and then can be used to sort data i.e
# print(df[(df.year == 2015) & (df.month == 5) & (df.dow == 4)])
# which will give you all the days in the month of May(df.month == 5),
# that fall on a Thursday(df.dow == 4), in the year 2015
# (df.year == 2015)
#
# Month Dow Year
# January = 1 Monday = 1 The year will be dispaly in a four
# February = 2 Tuesday = 2 digit format i.e. 2015
# March = 3 Wednesday = 3
# April = 4 Thursday = 4
# May = 5 Friday = 5
# June = 6
# July = 7
# August = 8
# September = 9
# October = 10
# November = 11
# December = 12
def year(x):
return(x.year)
def dow(x):
return(x.isoweekday())
def month(x):
return(x.month)
df.Date = df.Date.apply(dateutil.parser.parse)
df['year'] = df.Date.apply(year)
df['dow'] = df.Date.apply(dow)
df['month'] = df.Date.apply(month)


# The code below has a total of five sections all labeled by number.
# They are #1, #2, #3, #4, #5. Number one adds new columns to the df
# and populates them with data, number two filters out all the days
# that the market went down or flat for the day, number three filters
# out all of the days that the market went up or flat, number four
# filters all of the days that the market went up or down, and
# number five drops the excess columns and concats steps #2, #3, & #4.


# 1
# there are five columns that are being added, up_down, up, down,
# flat, and %chg. up, down, and flat are temporary and will be
# deleted later on the other two up_down, and %chg will be permeant.
# The up_down column is derived from taking the 'close' column minus the
# 'open'column, this tells you how much the stock has moved for the day.
# The 'up' column is temporary and has a value of 'up' for all the rows
# of the DataFrame df. The 'down' column is temporary and has a value of
# 'down' for all the rows of the DataFrame df. The 'down' column is
# temporary and has a value of 'flat' for all the rows of the DataFrame
# df. The '%chg' column is calculated by taking the results of the
# 'up_down' divided by the 'close' column, and then times 100, which
# turns it into a percentage show what percent the stock moved up or
# down for the day. All of the columns added below are added to the
# DataFrame called df, which contains a a csv file(see code lines 14-20
# for information on the csv file contained in the DataFrame df).
df['up_down'] = df['Close'] - df['Open']
df['up'] = 'up'
df['down'] = 'down'
df['flat'] = 'flat'
df['%chg'] = ((df['up_down']/df['Close'])*100)


# 2
# df column[up_down] is first filtered on the greater than zero
# criteria from the year 1984 on up and then is turned into df2.
# If the up_down column is greater than zero than this means that
# the stock went up. Next df3 is set = to df2['up'], df3 now holds
# just the days where the asset went up
df2= (df[(df.year > 1984) & (df.up_down > 0)])
df3 = df2['up']



# 3
# df column[up_down] is first filtered on the less than zero
# criteria from the year 1984 on up and then is turned into df4.
# If the up_down column is less than zero than this means that
# the stock went Down. Next df5 is set = to df4['down'], df5 now holds
# just the days where the asset went down
df4= (df[(df.year > 1984) & (df.up_down < 0)])
df5 = df4['down']



# 4
# df column[up_down] is first filtered on the equal to zero
# criteria from the year 1984 on up and then is turned into df6.
# If the up_down column is equal to zero than this means that
# the stock did not move. Next df7 is set = to df6['flat'],df5
# now holds just the days where the asset did not move at all
df6= (df[(df.year > 1984) & (df.up_down == 0)])
df7 = df6['flat']



# 5
# The code below starts by droping the columns 'up', 'down', and 'flat'.
# These were temporary and were used to help filter data in the above
# code in sections two, three, and four. Finally we concat the
# DataFrames df, df3, df5, and df7. We now have new 'up', 'down' and
# 'flat' columns that only display up, down, or flat when the criteria
# is true.
df = df.drop(['up'], axis = 1)
df = df.drop(['down'], axis = 1)
df = df.drop(['flat'], axis = 1)
df = pd.concat([df,df3,df5,df7],axis =1, join_axes=[df.index])


df['openchgprevday'] = ((df['Open']-1)-(df['Open'])

print("Hello World")


This is the error message

File "columnadder.py", line 144
print("Hello World")
^
SyntaxError: invalid syntax
Press any key to continue . . .

Answer

There is an extra parenthesis here

df['openchgprevday']  =   ((df['Open']-1)-(df['Open'])

print("Hello World")

To correct that, do

df['openchgprevday']  =   (df['Open']-1)-(df['Open'])

print("Hello World")
Comments