Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Last active January 3, 2021 20:22
Show Gist options
  • Save justinhchae/609681d183d7a3b9c35fb4fc33803561 to your computer and use it in GitHub Desktop.
Save justinhchae/609681d183d7a3b9c35fb4fc33803561 to your computer and use it in GitHub Desktop.
impute_dates
import pandas as pd
col1 = 'event_date'
col_new = str(col1 + '_new')
col2 = 'received_date'
curr_year = 2020
past_year = 2010
# given a dataframe df with col1 and col2 as datetime columns
# store the lambda function as an object
impute = lambda x: x[col1].replace(year=x[col2].year) if x[col1].year > curr_year \
else x[col1].replace(year=x[col2].year) if x[col1].year < past_year \
else x[col1]
# apply the lambda function
df[col_new] = df.apply(impute, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment