I'm trying to convert dates in a csv file from yyyyddd (where day is day/365 in the year) into yyyy-mm-dd. I've been trying to experiment with datetime, but it seems this functions works with data created in python, not in a csv that was imported in.
Asked
Active
Viewed 1,399 times
-2
-
1What are some of the things you have tried? – TheStrangeQuark Apr 03 '19 at 18:45
-
1Welcome to StackOverflow. To help you better we need some example data. 5 rows is enough to help you. See more information here about making a good `pandas` question: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Erfan Apr 03 '19 at 18:47
-
The format specifier for `ddd` is `%j` When reading the date in `YYYYddd` format, you would set your parser to `%Y%j` – Chris Charley Apr 03 '19 at 19:23
1 Answers
1
The datetime library can be used to parse the YYYYddd format into YYYY-mm-dd.
>>> from datetime import datetime
>>> date = '2019001'
>>> parsed = datetime.strptime(date, '%Y%j')
>>> print(datetime.strftime(parsed, '%Y-%m-%d'))
2019-01-01

Chris Charley
- 6,403
- 2
- 24
- 26