如何把int64中的12220转换成012220日期时间格式 [关闭] 。

问题描述 投票:0回答:1
Index(['Province/State', 'Country/Region', 'Lat', 'Long', '1/22/20', '1/23/20',
       '1/24/20', '1/25/20', '1/26/20', '1/27/20', '1/28/20', '1/29/20',
       '1/30/20', '1/31/20', '02-01-20', '02-02-20', '02-03-20', '02-04-20',
       '02-05-20', '02-06-20', '02-07-20', '02-08-20', '02-09-20', '02-10-20',
       '02-11-20', '02-12-20', '2/13/20', '2/14/20', '2/15/20', '2/16/20',
       '2/17/20', '2/18/20', '2/19/20', '2/20/20', '2/21/20', '2/22/20',
       '2/23/20', '2/24/20', '2/25/20', '2/26/20', '2/27/20', '2/28/20',
       '2/29/20', '03-01-20', '03-02-20', '03-03-20', '03-04-20', '03-05-20',
       '03-06-20', '03-07-20', '03-08-20', '03-09-20', '03-10-20', '03-11-20',
       '03-12-20', '3/13/20', '3/14/20', '3/15/20', '3/16/20', '3/17/20',
       '3/18/20', '3/19/20', '3/20/20', '3/21/20', '3/22/20', '3/23/20',
       '3/24/20', '3/25/20', '3/26/20', '3/27/20', '3/28/20', '3/29/20',
       '3/30/20', '3/31/20', '04-01-20', '04-02-20', '04-03-20', '04-04-20',
       '04-05-20'],
      dtype='object')

如何使用for循环将这些日期列转换为通用格式,即mmddddyy格式?

python date
1个回答
0
投票

这个代码做了一些假设。

  • 不同类型的日期格式在日期栏中的表示方式有两种 要么是mmddyy 要么是mm -dd -yy 而不是: 再也
  • 如果你只是想得到一个字符串列表,这些字符串在列中表示日期,并且遵循mmddyy的格式,那么访问cleaned_dates。
  • 如果你想从列中给定的日期中解析出一个datetime()对象的列表,那么访问date_objects。
  • 这段代码以一个字符串列表开始。你可以通过Index.values方法从Index对象中访问字符串列表(引用 https:/pandas.pydata.orgpandas-docsstablereferenceindexing.html。)

执行说明

代码会遍历每一个字符串。我定义了2个模式(斜线日期模式和连字符日期模式)。我看到日期字符串通过了哪种模式。例如,如果日期字符串通过了斜线_date_pattern,我们知道日期字符串的格式是mmddyy。strptime需要0个填充的月份和0个填充的日期字段,特别是当你分别使用%M和%d标志的时候(参见 https:/docs.python.org3librarydatetime.html#strftime-and-strptime-format-codes。). 所以我把字符串传给clean_date_str,以进一步格式化字符串,使其可以传给strptime。

import datetime as dt
import re
dates = ['1/22/20', '1/23/20',
       '1/24/20', '1/25/20', '1/26/20', '1/27/20', '1/28/20', '1/29/20',
       '1/30/20', '1/31/20', '02-01-20', '02-02-20', '02-03-20', '02-04-20',
       '02-05-20', '02-06-20', '02-07-20', '02-08-20', '02-09-20', '02-10-20',
       '02-11-20', '02-12-20', '2/13/20', '2/14/20', '2/15/20', '2/16/20',
       '2/17/20', '2/18/20', '2/19/20', '2/20/20', '2/21/20', '2/22/20',
       '2/23/20', '2/24/20', '2/25/20', '2/26/20', '2/27/20', '2/28/20',
       '2/29/20', '03-01-20', '03-02-20', '03-03-20', '03-04-20', '03-05-20',
       '03-06-20', '03-07-20', '03-08-20', '03-09-20', '03-10-20', '03-11-20',
       '03-12-20', '3/13/20', '3/14/20', '3/15/20', '3/16/20', '3/17/20',
       '3/18/20', '3/19/20', '3/20/20', '3/21/20', '3/22/20', '3/23/20',
       '3/24/20', '3/25/20', '3/26/20', '3/27/20', '3/28/20', '3/29/20',
       '3/30/20', '3/31/20', '04-01-20', '04-02-20', '04-03-20', '04-04-20',
       '04-05-20']

def clean_date_str(groups, separator):
    day = groups[1]
    month = groups[0]
    year = groups[2]

    #If the day field is a single digit
    #To allow strptime to work properly
    #We have to pad a 0 to the beginning 
    while len(day) != 2:
        day = '0' + day
    print(day)
    #If the month field is a single digit
    #To allow strptime to work properly
    #We have to pad a 0 to the beginning 
    while len(month) != 2:
        month = '0' + month
    print(month)
    # you can add padding for year as well.
    # But given the data is as above
    # there is no need to do so.
    return '/'.join([month, day, year])

#This data structure holds the list of cleaned dates in mm/dd/yy format
cleaned_dates = []

slash_date_pattern = re.compile(r'([\d]+)/([\d]+)/([\d]+)')
hyphen_date_pattern = re.compile(r'([\d]+)-([\d]+)-([\d]+)')

for date_str in dates:
    if slash_date_pattern.match(date_str):
        slash_object = slash_date_pattern.match(date_str)
        cleaned_dates.append(clean_date_str(slash_object.groups(), '/'))
    elif hyphen_date_pattern.match(date_str):
        hyphen_object = hyphen_date_pattern.match(date_str)
        cleaned_dates.append(clean_date_str(hyphen_object.groups(), '-'))

#This data_structure holds the datetime() objects for each date string present in input
date_objects = []
for date_str in cleaned_dates:
    print(dt.datetime.strptime(date_str, '%M/%d/%y'))
    date_objects.append(dt.datetime.strptime(date_str, '%M/%d/%y'))

更新

  • 给出了strptime如何正确解析日期字符串的参考链接。

0
投票

你可以使用strptime()将你的字符串转换为日期时间格式,并使用strftime()来获得所需的输出。

© www.soinside.com 2019 - 2024. All rights reserved.