如何将int64中的1/22/20转换为日期时间格式的01/22/20

时间:2020-05-12 15:47:37

标签: python numpy date

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循环将这些日期列转换为通用格式,即mm \ dd \ yy格式?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

此代码做出某些假设:

  • 在列中表示日期的日期类型的不同类型是mm / dd / yy或mm-dd-yy两种,而不再不再
  • 如果您只想要一列以日期表示的字符串列表,且格式为mm / dd / yy,请访问cleaned_dates
  • 如果要从列中给出的日期解析出datetime()对象的列表,请访问date_objects
  • 代码以字符串列表开头。您可以使用Index.values方法(参考https://pandas.pydata.org/pandas-docs/stable/reference/indexing.html)从Index对象访问字符串列表

实施说明

代码遍历每个字符串。我定义了2种模式(slash_date_pattern和hyphen_date_pattern)。我看到日期字符串通过哪种模式。例如,如果日期字符串传递给slash_date_pattern,则我们知道日期字符串的格式为mm / dd / yy。日期字符串尚未准备好传递给strptime,因为您可以看到有些字符串类似“ 1/22/20”。特别是如果分别使用%M和%d标志(参考https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes),strptime需要0个填充月份和0个填充日期字段。因此,我将字符串传递给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如何期望日期字符串正确解析它们的参考链接。
相关问题