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)