Currently I have a series of columns which I am creating that contain a boolean based on a date in the Dataframe I am using
df['bool1'] = [1 if x > pd.to_datetime('20190731') else 0 for x in df['date']]
df['bool2'] = [1 if x > pd.to_datetime('20190803') else 0 for x in df['date']]
df['bool3'] = [1 if x > pd.to_datetime('20190813') else 0 for x in df['date']]
I figured that a list comprehension like this is a pythonic way of solving the problem. I feel like my code is very clear in what it is doing, and somebody could easily follow it.
There is a potential improvement to be made in say creating a dictionary for {bool1:'20190731'} then loop through Key:Value pairs so that I don't repeat the line of code. But this will only decrease line number whilst increase readability and scalability. It won't actually made my code run faster.
However my problem is that this code is actually very slow to run. Should I be using a lambda function to speed this up? What is the fastest way to write this code?