0

I want to convert date column (datetime64[ns]) into day-month-year format with this code ,

pd.to_datetime(final['Date'],format = ('%d-%b-%Y'))

but it remains the same:

0     2019-12-31
1     2020-01-01
2     2020-01-02
3     2020-01-03
4     2020-01-04
         ...    
112   2020-04-21
113   2020-04-22
114   2020-04-23
115   2020-04-24
116   2020-04-25
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Smbat
  • 71
  • 1
  • 8
  • you use `to_datetime` to parse string to datetime. the other way round (datetime to string), you use `strftime`. Besides that, yyyy-mm-dd is just how `pandas` displays a date in a df, with a column of dtype datetime64[ns]. if you want the column to be dtype datetime64[ns], I guess you have to live with that. – FObersteiner May 06 '20 at 14:28
  • @MrFuppes do you know any way to change format into dd-mm-yyyy but keep type as datetime ? It is not necessary datetime64[ns] – Smbat May 06 '20 at 15:08
  • I think not, see also e.g. [this](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) question... Why do you want to change it? It's just a matter of display... – FObersteiner May 06 '20 at 15:32
  • @MrFuppes because in Armenia we use dd-mm-yyyy format – Smbat May 25 '20 at 21:26
  • well, in Germany, we normally use dd.mm.yyyy - however for working with date and time, I would always prefer [ISO format](https://en.wikipedia.org/wiki/ISO_8601)! If you want to display data for a presentation etc., you're right, it is better to use *local* conventions. For that, you have `strftime()` – FObersteiner May 26 '20 at 07:10

1 Answers1

0

you can use

df['Date'] = df['Date'].dt.strftime('%d-%m-%Y')

are you spelling month wrong? it says -%b instead of -%m

DucksEatTurtles
  • 194
  • 1
  • 2
  • 10
  • it worked , but dtype changed into object . How can I keep it as datetime64[ns] or something like that – Smbat May 06 '20 at 13:33