How to convert integer into date object python?

I would suggest the following simple approach for conversion: from datetime import datetime, timedelta s = “20120213” # you could also import date instead of datetime and use that. date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8])) For adding/subtracting an arbitary amount of days (seconds work too btw.), you could do the following: date += timedelta(days=10) date -= … Read more

Changing date format in R

There are two steps here: Parse the data. Your example is not fully reproducible, is the data in a file, or the variable in a text or factor variable? Let us assume the latter, then if you data.frame is called X, you can do X$newdate <- strptime(as.character(X$date), “%d/%m/%Y”) Now the newdate column should be of … Read more

What are the “standard unambiguous date” formats for string-to-date conversion in R?

This is documented behavior. From ?as.Date: format: A character string. If not specified, it will try ‘”%Y-%m-%d”‘ then ‘”%Y/%m/%d”‘ on the first non-‘NA’ element, and give an error if neither works. as.Date(“01 Jan 2000”) yields an error because the format isn’t one of the two listed above. as.Date(“01/01/2000”) yields an incorrect answer because the date … Read more

A faster strptime?

Is factor 7 lot enough? datetime.datetime.strptime(a, ‘%Y-%m-%d’).date() # 8.87us datetime.date(*map(int, a.split(‘-‘))) # 1.28us EDIT: great idea with explicit slicing: datetime.date(int(a[:4]), int(a[5:7]), int(a[8:10])) # 1.06us that makes factor 8.

Get date from week number

A week number is not enough to generate a date; you need a day of the week as well. Add a default: import datetime d = “2013-W26” r = datetime.datetime.strptime(d + ‘-1’, “%Y-W%W-%w”) print(r) The -1 and -%w pattern tells the parser to pick the Monday in that week. This outputs: 2013-07-01 00:00:00 %W uses … Read more