简体   繁体   English

根据月份和月份创建日期和日期列

[英]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.

相关问题 如何从月份和年份列创建日期时间索引,但没有日期? - How to create datetime index from month and year columns, but without day? 如何为年/月/日/小时/分钟/秒创建日期时间的Pandas列? - How to create a Pandas column for datetime from year / month/ day / hour / minute / second? 根据年和月列创建具有每月第 7 个工作日的新列 - Create new column with 7th business day of the month based on year and month columns 将年,月和日转换为日期时间 - Converting year, month and day to datetime Python:根据“年”、“月”、“日”列实现包含星期几的列? - Python: Implement a column with the day of the week based on the “Year”, “Month”, “Day” columns? 从datetime列中提取月份中的日期作为数组 - Extract day of month as array from datetime column 从其他年月日列中创建 python 中的 datetime.date 列 - Make datetime.date column in python from other year,month and day column 如何将年、月和日列组合为单个日期时间列? - How to combine year, month, and day columns to single datetime column? 从另一个 dataframe 基于年月日的日期时间索引 dataframe 行删除 - Drop from datetime index dataframe rows based in year month day from another dataframe 从 arrays 和年、月、日的单个值创建 numpy 日期时间格式 - Create numpy datetime format from arrays and single values for year, month and day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM