【问题标题】:How to convert 1/22/20 in int64 to 01/22/20 datetime format [closed]如何将 int64 中的 1/22/20 转换为 01/22/20 日期时间格式 [关闭]
【发布时间】:2023-03-04 14:32:01
【问题描述】:
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 格式?

【问题讨论】:

  • 不是machine-learniing 问题 - 请不要向无关标签发送垃圾邮件(已删除)。
  • 据我目前所见,所有输入的格式均为 mm\dd\yy 或 mm-dd-yy。你想要的只是用'\'替换'-'?
  • “int64 中的 1/22/20”是什么意思?

标签: python numpy date


【解决方案1】:

这段代码做了一些假设:

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

实施说明

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2023-01-13
      • 2019-07-02
      • 1970-01-01
      • 2015-06-03
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      相关资源
      最近更新 更多