Skip to content
Advertisement

Is it possible to pass an extra argument to lambda function in pandas read_csv

I am using the read_csv() function from pandas and the option for a lambda date_parser function quit often and I am wondering if it is possible to pass an argument to this labda function.

This is a minimal example where I set the format_string:

JavaScript

I do know, that pandas has a infer_datetime_format flag, but this is question is only looking for a self defined date_parser.

Advertisement

Answer

Welcome to the magic of partial functions.

JavaScript

Basically you define your function inside a function and return that inner function as the result. In this case I call outer(5) which means I now have a function assigned to fn that I can call lots of times, each time it will execute the inner function, but with the outer_arg in the closure.

So in your case:

JavaScript

Once you know how this works, there is a shortcut utility:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement