Pandas apply returns function instead of value


Puzzling pandas apply behavior

data = {'date_col_1': ['2020-01-24', 
                       '2020-03-24' ],
        'date_col_2': ['2017-03-08', 
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

>Solution :

The lambda isn’t necessary. You should use:

testdf.apply(pd.to_datetime, axis=1)

Leave a Reply Cancel reply