【问题标题】:Python DataFrame How to split or extract date from a datetime stampPython DataFrame如何从日期时间戳中拆分或提取日期
【发布时间】:2019-01-27 10:48:21
【问题描述】:

我想从日期时间戳中提取日期以写入 Print 函数以用作图的标题。以下是我的代码:

plt.title('%s day IV curves of sample Module'%(module_allData_df['Time'].loc[i].replace(hour=0,minute=0,second=0,microsecond=0)))

输出是:

但我希望标题为:

2016-08-13 day IV curves of sample Module

我不希望时间戳出现在那里。怎么做?

我已经按照@jezrael 接受的答案修改了代码并且它有效。这是代码和解决方案:

plt.title('%s day IV curves of sample Module'%(module_allData_df['Time'].loc[i].strftime('%Y-%m-%d')))

输出是:

【问题讨论】:

    标签: python python-3.x pandas datetime matplotlib


    【解决方案1】:

    使用Timestamp.strftime:

    val = module_allData_df['Time'].loc[i].strftime('%Y-%m-%d')
    
    plt.title('{} day IV curves of sample Module'.format(val))
    

    对于python 3.6+:

    plt.title(f'{val} day IV curves of sample Module')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2014-05-02
      • 1970-01-01
      • 2020-10-05
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多