inaMinute
inaMinute

Reputation: 585

how to pass the parameters to apply correctly

There is an api function

get_next_trading_date(exchange='SZSE', date='2017-05-01')

and I have a DataFrame backTestRecordAfterModified showed as follow

enter image description here when I run

backTestRecordAfterModified['createdAt']=backTestRecordAfterModified['createdAt'].apply(func=get_next_trading_date, exchange='SZSE')

the console displayed the message : TypeError: get_next_trading_date() got multiple values for argument 'exchange'

so,how to pass the parameters correctly

supplementary

backTestRecordAfterModified['createdAt'] = backTestRecordAfterModified['createdAt'].apply(lambda date: get_next_trading_date(date, exchange='SZSE'))

the code above still displays the same error.

enter image description here

i add the definition of get_next_trading_date

enter image description here

I got the final answer just now.

backTestRecordAfterModified['createdAt']=backTestRecordAfterModified['createdAt'].apply(lambda date: get_next_trading_date(date=date,exchange='SZSE'))

Upvotes: 0

Views: 138

Answers (3)

applesoup
applesoup

Reputation: 487

You'll have to use a lambda-function to pass the additional parameter to the get_next_trading_date() function:

backTestRecordAfterModified['createdAt']=backTestRecordAfterModified['createdAt'].apply(lambda date: get_next_trading_date(date=date, exchange='SZSE'))

The pandas.Series.apply() function does in fact support additional keyword arguments for the function, but the first argument to the function is always the value from the pandas series.

If get_next_trading_date() was defined differently, with the order of arguments reversed:

get_next_trading_date_2(date='2017-05-01', exchange='SZSE')

you could have used

backTestRecordAfterModified['createdAt']=backTestRecordAfterModified['createdAt'].apply(func=get_next_trading_date, exchange='SZSE').

Upvotes: 1

jpp
jpp

Reputation: 164783

One option is to use df.apply instead of series.apply:

df['createdAt'] = df.apply(lambda row: get_date(row['createdAt'], 'SZSE'), axis=1)

Or, if you don't want to pass the whole dataframe:

df['createdAt'] = [get_date(x, 'SZSE') for x in df['createdAt'].values]

Upvotes: 0

Thirupathi Thangavel
Thirupathi Thangavel

Reputation: 2465

The apply function is called for each value of the pandas series, and the value is passed as an argument to the function by default.

The additional arguments you specify are passed after the series value. So in your example, each time the function call would be like

get_next_trading_date(<i-th value of the series>, exchange='SZSE')

But in your function, the first argument is for exchange, so the <i-th value of the series> (current date) is passed to exchange and then there's another keyword argument trying to set the same variable. This causes the error. More in here.

Here you have two options.

a) Change the function definition to take date as first argument, so that you don't have to change your function call. But be sure to make the change everywhere you call this function.

get_next_trading_date(date='2017-05-01', exchange='SZSE')

b) Change your function call to pass date as second argument.

backTestRecordAfterModified['createdAt'] = backTestRecordAfterModified['createdAt'].apply(lambda date: get_next_trading_date(date, exchange='SZSE'))

Or simplified as,

backTestRecordAfterModified['createdAt'].apply(lambda date: get_next_trading_date(date, exchange='SZSE'), inplace=True)

Upvotes: 0

Related Questions