Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert panda date time to particular date format and then extract substring out of it

I have this date time column in panda. I am converting it to specific date format. i want to convert this date into string and extract the substring out of it. I am finding it hard to do it. does anyone know how.

data in my datetime column looks like follows:

2024-01-30 13:45:00
2024-01-30 14:45:00
2024-01-30 15:45:00

I have converted it to date column using following statement:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

da['date1'] = da['timestamp'].dt.strftime("%y%b%d")

Now my data looks like following:

24Jan30
24Jan30
24Jan30

Note this is in date format.

I want to extract year and month and from it. for that i have to convert it into string which is not giving me expected results.

expected output:

24Jan
24Jan
24Jan

Any help would be greatly appreciated.
Thanks in advance.

>Solution :

Just remove the %d (day of the month) at the end of your statement, like this:

da['date1'] = da['timestamp'].dt.strftime("%y%b")

%y: year without century

%b: 3-letter abbreviation of month name

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading