如何将年,月,日列表转换为日期时间列表? [英] How to convert lists of years, months, days into list of datetimes?

查看:238
本文介绍了如何将年,月,日列表转换为日期时间列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个温度数据的pandas数据框,其中包含年,月,日和温度的列标题.对天和月进行编号而不是标记(即第一行=年:1981,月= 1,天= 1;第二行=年:1981,月= 1,天= 2;以此类推)

I have a pandas dataframe of temperature data with column headers of year, month, day, and temperature. The days and months are numbered instead of labeled (i.e. first row = year: 1981, month = 1, day = 1; second row = year: 1981, month = 1, day = 2; etc)

如何将这些年,月和日(特别是后两个)的单独列表转换为以下格式的日期时间:

How do I go about converting these individual lists of years, months, and days (specifically the latter two) into datetimes of the format:

第一行日期= 1981年1月1日;第二行日期= 1981年1月2日;等等

first row date = Jan 1, 1981; second row date = Jan 2, 1981; etc

我基本上想将三列日期片段压缩为一列日期.

I basically want to condense the three columns of date pieces into one column of dates.

感谢您的帮助!

推荐答案

这有效:

import datetime

date = datetime.date(year, month, day)

# For a dataframe:
df['date'] = df.apply(lambda row: datetime.date(row['year'], row['month'], row['day']), axis=1)

这篇关于如何将年,月,日列表转换为日期时间列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆