[英]Create datetime column from month and day with year based on month
I have columnar data of dates of the form mm-dd as shown. 我有mm-dd形式的日期列数据,如图所示。 I need to add the correct year (dates October to December are 2017 and dates after 1-1 are 2018) and make a datetime object.
我需要添加正确的年份(10月到12月的日期是2017年,1-1以后的日期是2018年)并创建datetime对象。 The code below works, but it's ugly.
下面的代码有效,但是很丑陋。 Is there a more Pythonic way to accomplish this?
有没有更Python化的方式来做到这一点?
import pandas as pd
from datetime import datetime
import io
data = '''Date
1-3
1-2
1-1
12-21
12-20
12-19
12-18'''
df = pd.read_csv(io.StringIO(data))
for i,s in enumerate(df.Date):
s = s.split('-')
if int(s[0]) >= 10:
s = s[0]+'-'+s[1]+'-17'
else:
s = s[0]+'-'+s[1]+'-18'
df.Date[i] = pd.to_datetime(s)
print(df.Date[i])
Prints: 打印:
2018-01-03 00:00:00
2018-01-02 00:00:00
2018-01-01 00:00:00
2017-12-21 00:00:00
2017-12-20 00:00:00
2017-12-19 00:00:00
2017-12-18 00:00:00
You can conver the date to pandas datetime
objects. 您可以将日期转换为pandas
datetime
对象。 Then modify their year with datetime.replace
. 然后使用
datetime.replace
修改其年份。 See docs for more information. 请参阅文档以获取更多信息。
You can use the below code: 您可以使用以下代码:
df['Date'] = pd.to_datetime(df['Date'], format="%m-%d")
df['Date'] = df['Date'].apply(lambda x: x.replace(year=2017) if x.month in(range(10,13)) else x.replace(year=2018))
Output: 输出:
Date
0 2018-01-03
1 2018-01-02
2 2018-01-01
3 2017-12-21
4 2017-12-20
5 2017-12-19
6 2017-12-18
This is one way using pandas
vectorised functionality: 这是使用
pandas
矢量化功能的一种方式:
df['Date'] = pd.to_datetime(df['Date'] + \
np.where(df['Date'].str.split('-').str[0].astype(int).between(10, 12),
'-2017', '-2018'))
print(df)
Date
0 2018-01-03
1 2018-01-02
2 2018-01-01
3 2017-12-21
4 2017-12-20
5 2017-12-19
6 2017-12-18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.