For memos.
Process date / time columns with pandas (string conversion, date extraction, etc.)
import datetime
from dateutil.relativedelta import relativedelta
#Make one term later
df["period1"] = df["Settlement date"]
df_target = df[["Ticker","period1","Market capitalization"]].copy()
df_target.columns = ["Ticker","period1","period1ago"]
df_target["period1"] = df_target["period1"].dt.to_pydatetime()+ relativedelta(months=3)
df = pd.merge(df,df_target,how="inner",on=("Ticker","period1"))
df
Recommended Posts