Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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', 
                       '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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>>pd.__version__ : 1.5.2

>Solution :

The lambda isn’t necessary. You should use:

testdf.apply(pd.to_datetime, axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading