python以日期格式更改月份和日期的顺序

时间:2018-01-13 09:03:21

标签: python

我有一个显示日期的列 2016/26/2(日/月/日)

如何将其显示为2016年2月26日(月/日)?

[in] print(nir['Date'])
[out] 0    2005-01-12
      1    2006-01-03
      2    2006-01-06
      3    2006-01-09
      4    2006-01-12
      5    2007-01-03
      6    2007-01-06
      7    2007-01-09
      8    2007-01-12

我试图这样做:

      nir.Date = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), nir.Date)]

但没有运气...... 有没有办法轻松交换月和日的地方而不转换为字符串?

1 个答案:

答案 0 :(得分:3)

python/pandas中,如果不想转换为字符串则不可能。

对于字符串,请strftime使用formats

nir.Date = nir.Date.dt.strftime('%m/%d/%Y')