简体   繁体   English

Python Dataframe 将日期时间解析为年、月、日、时、分、秒的列

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

I have a datetime column ['time_in'] in a dataframe.我在 dataframe 中有一个日期时间列 ['time_in']。 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)?下面的 2 个代码有效,但为了效率或简洁,是否有更紧凑或预先存在的 function 来分解为年、月、日、小时、分钟、秒的列(或者更好的是,这些组件的任何子集)?

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)
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)在 90k 行上进行 500 次迭代 = 每次迭代平均 073 秒比上述 v1 快 30% (单独调用)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Python解析年,月,日,时,分,秒 - Parsing year, month, day, hour, minute, second in Python python中的时间戳(年、月、日、小时、分钟) - Timestamp in python (year,month,day,hour,minute) 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? 如何从日期时间对象中提取小时/分钟并去掉年/月/日/秒部分? - How do I extract the Hour/Minute from a datetime object and get rid of the Year/Month/Day/Second section? 如何为年/月/日/小时/分钟/秒创建日期时间的Pandas列? - How to create a Pandas column for datetime from year / month/ day / hour / minute / second? 如何将年、月、日、小时/分钟列转换为单个日期时间列? - How to convert year, month, day, hour/minute columns into a single datetime column? 从当前日期时间中提取年、月、日、小时和分钟 - Extracting year, month, day, hour and minute from the current datetime Python datetime - 在使用 strptime 获取日、月、年之后设置固定的小时和分钟 - Python datetime - setting fixed hour and minute after using strptime to get day,month,year 按分钟,小时,天,月和年分组? - groupby minute, hour, day, month, and year? 这个时间格式是什么:XX:XX.X,以及如何在 Python 中将其转换为通常的年月日时分秒 - what is this time format: XX:XX.X, and how to transfer it to usual year-month-day-hour-minute-second in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM