0

I have a dataset which contains some columns with datetime. My problem is, I found this type datetime format:

Apr'11
Apr-11
Apr 11

How can I automatically change this format to be datetime format?

1 Answer 1

2

for you can use datetime module

from datetime import datetime 

this is the link if you want any confusion

date_string = "Apr'11"
date = datetime.strptime(date_string, "%b'%d")

%b = Locale’s abbreviated month name. (like Apr, Mar, Jan, etc) %d = Day of the month as a decimal number [01,31]

date_string_2 = "Apr-11"
date = datetime.strptime(date_string, "%b-%d")

date_string_3 = "Apr 11"
date = datetime.strptime(date_string, "%b %d")

You should write this "%b %d" same as like date_string otherwise it will give you, even if you give an extra space.

go to this link to learn more about this: https://docs.python.org/3/library/time.html

Sign up to request clarification or add additional context in comments.

3 Comments

that's mean I can do it one by one. My problem is I have some others like that, such as Jan-Des and from 11-15. How can I do change it automatically?
If you have 5 to 6 date formats, then you can use for loop for date in dates and in that for loop you can use if-else condition, it will automatically change if you have some
can you write down how is the code, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.