ZacAttack ZacAttack - 2 months ago 57
Python Question

Why is my function that I made getting the TypeError: f() takes 0 positional arguments but 1 was given

I am working with pandas DataFrames and I am adding new columns for more advanced analysis. My f function is giving me an error TypeError: f() takes 0 positional arguments but 1 was given. I can't figure out why, I have my f function documented in the code if you need to know what it does.

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



# 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 date 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")



# 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 Day 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)

# f is a function that checks to see if the up_down column
# has a value that is greater than, less than, or equal to
# zero. The value in the up_down column is derived from
# subtracting the opening price of the stock(open column)
# from closing price of the stock(close column). If up_down
# has a negative value than the stocks price was Down, a positive
# value then Up, and no change is Flat
def f():
if up_down > 0:
x = Up
elif up_down < 0:
x = Down
else:
x = Flat
return (f)


df.reset_index()

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)
df['up_down'] = df['Close'] - df['Open']
df['up_down_flat'] = df.up_down.apply(f)


df2= (df[(df.year > 1984) & (df.month == 5) & (df.dow == 1)])
print (df2)


This is the error

Traceback (most recent call last):
File "dframt1est.py", line 77, in <module>
df['up_down_flat'] = df.up_down.apply(f)
File "C:\Users\Zac\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\core\series.py", line 2220, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas\src\inference.pyx", line 1088, in pandas.lib.map_infer (pandas\lib.c:63043)
TypeError: f() takes 0 positional arguments but 1 was given
Press any key to continue . . .

Answer

You're passing f as the function in apply. That function is called for each row in the dataframe, and needs to take the row as its parameter.

Note also that you're returning the function itself as the result; I'm pretty sure you meant to return x not f. Also, you don't seem to have defined Up, Down and Flat anywhere.

Comments