Python Dataframe parse datetime into columns for year, month, day, hour, minute, second

Viewed 22

I have a datetime column ['time_in'] in a dataframe. The below 2 codes work, but for efficiency or brevity, is there a more compact or pre-existing function to break into columns of year, month, day, hour, minute, second (or better yet, any subset of those components)?

df['year']=df.time_in.dt.year.astype('Int64')
df['month']=df.time_in.dt.month.astype('Int64')
df['day']=df.time_in.dt.day.astype('Int64')
df['hour']=df.time_in.dt.hour.astype('Int64')
df['minute']=df.time_in.dt.minute.astype('Int64')
df['second']=df.time_in.dt.second.astype('Int64')

Or this

df['dt_string']=df.time_in.astype(str)
dfx=df.dt_string.str.split(expand=True)
dfdate=pd.DataFrame(columns=['year','month','day'])
dftime=pd.DataFrame(columns=['hour','minute','second'])
dfdate[['year','month','day']]=dfx[0].str.split('-',expand=True).astype('int64')
dftime[['hour','minute','second']]=dfx[1].str.split(':',expand=True).astype(float).astype('int64')
df=pd.concat([df,pd.concat([dfdate,dftime], axis=1)], axis=1)
1 Answers
attr = ['year','month','day','hour','minute','second']
for a in attr:
    df[a]=getattr(df.time_in.dt,a)

500 iterations on 90k rows= average .073s per iteration 30% faster than v1 above (individual calls)

or in a functional form

def dt_split(df,col,times=['year','month','day','hour','minute','second']):
    for ttt in times:
        df[ttt]=getattr(df[col].dt,ttt)
    return df
Related