I have a string column which holds dates in the format in '2015-04-13' format in a csv file. How do I change it to date type so that I can change the format to "mm-dd-yyyy".
Asked
Active
Viewed 31 times
0
-
1Do you just want to convert a string `yyyy-mm-dd` to another string `mm-dd-yyyy`? Or do you need the Date object? – Mathieu David Jun 04 '15 at 11:39
-
If you don't really need the Date object you can do the conversion using string slicing & concatenation. OTOH, converting via a Date object allows you to verify that your date strings are actually valid dates. – PM 2Ring Jun 04 '15 at 11:42
-
I think this question is legitimate, if I understand it correctly the OP wants to convert a **string** from format `yyyy-mm-dd` to `mm-dd-yyyy` but thought that he had to convert to Date object to do so. Which is not the case if he doesn't need to perform other operations on the date. – Mathieu David Jun 04 '15 at 11:49
-
@MathieuDavid can we change the date format without changing the string to a date format ?? – satyaki Jun 04 '15 at 11:56
-
Yes if you don't need to use the date object to perform other operations you can just use `result = date[5:7] + "-" + date[8:10] + "-" + date[:4]` to convert from one format to the other. If you wonder how it works, it's called slicing. You can read about it in [the docs](https://docs.python.org/2/tutorial/introduction.html#strings) (scroll down a little) – Mathieu David Jun 04 '15 at 12:00