I have a column with dates in it in the string format as'08-MAY-17'. How can I convert this column into datetime format so that I can select a specific time window for my datafrme
Asked
Active
Viewed 52 times
-2
-
2If you have a problem you can post what you've tried with a clear explanation of what isn't working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). I suggest reading [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Also, be sure to take the [tour](https://stackoverflow.com/tour) – Yash Karanke Jun 26 '17 at 16:16
2 Answers
1
Consider the dataframe df
df = pd.DataFrame(dict(Date=['08-MAY-17'] * 10))
pd.to_datetime
can handle that format just fine.
df.Date = pd.to_datetime(df.Date)
df
Date
0 2017-05-08
1 2017-05-08
2 2017-05-08
3 2017-05-08
4 2017-05-08
5 2017-05-08
6 2017-05-08
7 2017-05-08
8 2017-05-08
9 2017-05-08

piRSquared
- 285,575
- 57
- 475
- 624
-
Thanks a lot! Will this method work if datetime is in '13-MAY-16 01.22.47.000000 AM AMERICA/LOS_ANGELES' format? Or I have to split the string first? – Chinmay Ajnadkar Jun 26 '17 at 21:29
-
I can handle the format `'13-MAY-16 01.22.47.000000 AM'` with `pd.to_datetime(df.Date, format='%d-%b-%y %I.%M.%S.%f %p')` But I don't know what to do with the timezone at the moment. You should search or ask another question. – piRSquared Jun 26 '17 at 21:36
1
You can use pd.to_datetime
s = "08-MAY-17"
pd.to_datetime(s)
Out[87]: Timestamp('2017-05-08 00:00:00')
Read more in the documentation
EDIT:
piRSquared's answer shows the case when the input to the method is a dataframe column

akilat90
- 5,436
- 7
- 28
- 42
-
This doesn't address the issue of having a column of dates and what this answer does address is identical to the solution I provided 2 minutes before you posted your answer. – piRSquared Jun 26 '17 at 16:39
-
@piRSquared thanks for pointing that out and I have edited the answer. As for the similarity of the answer you posted 2 minutes before mine: I didn't see your answer by the time I started writing this. – akilat90 Jun 26 '17 at 17:34
-
1
-