Sanito Machiavelli
Sanito Machiavelli

Reputation: 133

python change order of month and day in date format

I have a column that displays date as 2016/26/2 (d/m/y)

How can i display it as 2/26/2016 (m/d/y)?

[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

i tried to do this:

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

but no luck... is there any way to easily swap places of month and day without converting to strings?

Upvotes: 3

Views: 2763

Answers (1)

jezrael
jezrael

Reputation: 862801

In python/pandas it is not possible if dont want convert to strings.

For strings use strftime with formats:

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

Upvotes: 4

Related Questions