Skip to content

Instantly share code, notes, and snippets.

@justinhchae
justinhchae / impute_dates_iter_change_log.py
Last active December 30, 2020 22:07
impute dates with iterative solution and change log
# same iterrows() solution with change log
curr_year = 2020
past_year = 2010
change_log = []
# loop through index and rows of df
for idx, row in df.iterrows():
if row[col1].year > curr_year:
new_year = row[col2].year
# save reference data as tuple
records = tuple((row['case_id']
@justinhchae
justinhchae / impute_dates_iteration.py
Created December 30, 2020 22:01
impute with iteration
# iterate through a DataFrame with iterrows()
curr_year = 2020
past_year = 2010
# loop through index and rows of df
for idx, row in df.iterrows():
# compare years as integers
if row[col1].year > curr_year:
# return received_date's year as integer
new_year = row[col2].year
@justinhchae
justinhchae / impute_dates.py
Last active January 3, 2021 20:22
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