Pandas apply returns function instead of value

Question:

Puzzling pandas apply behavior

data = {'date_col_1': ['2020-01-24', 
                       '2020-03-24' ],
 
        'date_col_2': ['2017-03-08', 
                       '2020-01-24']}
 
testdf = pd.DataFrame(data)

Then try to convert the columns to datetime,

>>>testdf.apply(lambda x: pd.to_datetime, axis=0)
0    <function to_datetime at 0x1170c2f80>
1    <function to_datetime at 0x1170c2f80>
dtype: object

Why is apply returning function instead of return value ?

>>>pd.__version__ : 1.5.2

Asked By: muon

||

Answers:

The lambda isn’t necessary. You should use:

testdf.apply(pd.to_datetime, axis=1)
Answered By: BrokenBenchmark
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.