简体   繁体   English

Python-Pandas:使用年,年中的星期和星期几构造日期

[英]Python - Pandas: Construct date with year, week of year and day of week

Is there a way, using pandas, to construct a Timestamp based on 3 input: 有没有一种方法,可以使用熊猫根据3种输入构造Timestamp

  • the year (ex: 2010) 年(例如:2010年)
  • the week of the year (ex: 3 : the 3rd week) 一年中的一周(例如:3:第三周)
  • the day of the week: (for instance Monday) 星期几:(例如星期一)

In that particular example, the result would be Timestamp('2010-01-18') 在该特定示例中,结果将为Timestamp('2010-01-18')

Anyone have a hint of a clever way to achieve this? 任何人都有实现这一目标的聪明方法的暗示吗?

You can generate a date using the logic you've provided like this: 您可以使用提供的逻辑生成日期,如下所示:

>>> datetime.datetime.strptime("2010-3-Monday", "%Y-%W-%A")
datetime.datetime(2010, 1, 18, 0, 0)

You may need to adjust your format, depending on what, exactly, you are passing to strptime , but you can find the appropriate formatting options here 您可能需要调整格式,具体取决于传递给strptime ,但是您可以在此处找到适当的格式设置选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM