【问题标题】:Convet W(Number)-Y(NUMBER) to date in Python [closed]在 Python 中将 A(Number)-Y(NUMBER) 转换为日期 [关闭]
【发布时间】:2023-03-06 17:59:01
【问题描述】:

使用 Python:

我们如何将一系列日期 W(Number)-Y(NUMBER) 转换为以下格式 'YYYY-MM-DD'

例子:

W01-16
W02-16
W03-16
...

收件人:

2016-01-04
2016-01-11
2016-01-18
...

【问题讨论】:

  • 在寻求帮助之前,已经尝试/研究了什么?

标签: python date datetime datestamp


【解决方案1】:

改编自Get date from week number

  • %w 代表星期几,1 代表星期一
  • %W 一年中的一周
  • %y 表示 2 位数的年份
from datetime import datetime

d = "W01-16"
print(datetime.strptime('1-' + d, "%w-W%W-%y"))  # 2016-01-04 00:00:00

from datetime import datetime
from pandas import Series

s = Series(["W01-16", "W02-16", "W03-16"])
s = s.apply(lambda d: datetime.strptime('1-' + d, "%w-W%W-%y"))
print(s)

【讨论】:

  • 这是一长串日期。我在示例中显示的 DD 与今天有关。您能否展示一个可扩展的解决方案?
  • @InVinci 你是什么意思?你只需要在这里传递你的格式
  • @azro 您的解决方案抛出此错误 -- TypeError: strptime() argument 1 must be str, not Series
  • @InVinci 你可能需要学习如何在系列上应用方法,我会更新我的代码
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
相关资源
最近更新 更多