-1

Have dd-mm-yy in "date" column

05-01-15

need yyyy-mm-dd

2015-01-05

Solved with

df['date'] = pd.to_datetime(df.date, format='%d-%m-%y', errors='coerce')

Has it another solution?

  • @Sayse there must be plenty of answers to this, but the link posted is different because it's about python in general, and the question is about pandas. – delimiter Mar 01 '21 at 19:57

2 Answers2

1
from datetime import datetime

now = datetime.now() 

date_time = now.strftime("%Y-%m-%d")
print("date and time:",date_time)

Or have a closer look at strftime documentation.

Shraft
  • 332
  • 1
  • 5
  • Excuse me, may be I'm didn't got it, but why "now"? I have 2015 in the table. And all what's necessary to change the order of D-M-Y to Y-M-D. – Danil Zyryanov Mar 05 '21 at 15:53
-1
df['date'] = pd.to_datetime(df['date'])

Will transform your custom format to pandas recognized datetime, which you will be able to use for calculations.

delimiter
  • 745
  • 4
  • 13
  • 1
    "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer). This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – John Conde Mar 01 '21 at 22:11
  • Thanks @JohnConde, I'll check the link and provided more context in the meantime. – delimiter Mar 01 '21 at 22:15