【问题标题】:ValueError: time data 'temp_month/temp_year/1' does not match formatValueError:时间数据“temp_month/temp_year/1”与格式不匹配
【发布时间】:2019-07-30 03:58:58
【问题描述】:

我正在使用 Python 3.7.1

我在格式化日期时遇到问题。我必须以月/日/年的格式打印三个值。

我从数据框中获取年份并存储在 temp_year 中,类似地,月份和日期默认为 1。所以我想以 temp_month/1/temp_year 格式打印。

temp_year=(input_df["Indexation Date"].dt.year).iloc[0]
temp_year=temp_year-1
temp_month=(input_df["Indexation Date"].dt.month).iloc[0]
temp_month=temp_month-rpi_lag_months

s="temp_month/temp_year/1"

index_start_date=datetime.strptime(s,"%m/%d/%Y")

我希望输出是:

3/1/2012

但是得到的错误是:

值错误:时间数据“temp_month/temp_year/1”与格式“%m/%d/%Y”不匹配

【问题讨论】:

    标签: python


    【解决方案1】:

    您不传递字符串,因为它不包含任何值。而且你的格式也不匹配。

    尝试更改此设置

    s="temp_month/temp_year/1" 
    

    s=f"{temp_month}/1/{temp_year}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2018-02-15
      • 2017-03-05
      • 2014-09-27
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多