Last active
January 3, 2021 20:22
-
-
Save justinhchae/609681d183d7a3b9c35fb4fc33803561 to your computer and use it in GitHub Desktop.
impute_dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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