Python – how to convert and determine a type of a 5 digit date?
Question:
I have some of the date fields represented as 5-digit numbers. And there is a mapping from the numbers to an actual date. However I can’t figure out what logic should be applied to convert the numbers to dates in the "%Y-%m-%d"
format?
13581 -> 2007-03-09
12784 -> 2005-01-01
Answers:
As shown by
import datetime
datetime.date(2005, 1, 1)-datetime.timedelta(days=12784)
# datetime.date(1970, 1, 1)
your number is the number of days since 1970-01-01.
So, you can get the date by:
datetime.date(1970, 1, 1) + datetime.timedelta(days=12784)
# datetime.date(2005, 1, 1)
Here you go:
>>> from datetime import date, timedelta
>>> (date(1970,1,1) + timedelta(days=12784)).strftime("%Y-%m-%d")
'2005-01-01'
The numbers shown are the numbers of days since 1st January 1970, which is the origin of Unix time.
They can be converted using for example:
from datetime import datetime, timedelta
n = 13581
print((datetime.utcfromtimestamp(0) + timedelta(n)).strftime("%Y-%m-%d"))
gives:
2007-03-09
Here timedelta
is called with a single argument, being the offset in days. In general it is called with timedelta(days, seconds, microseconds)
but all of these arguments default to zero.
Just in case for a column in dataframe while using the accepted answer, you can use .apply
:
df["column"].apply(lambda x: (datetime.utcfromtimestamp(0) + timedelta(int(x))).strftime("%Y-%m-%d"))
I have some of the date fields represented as 5-digit numbers. And there is a mapping from the numbers to an actual date. However I can’t figure out what logic should be applied to convert the numbers to dates in the "%Y-%m-%d"
format?
13581 -> 2007-03-09
12784 -> 2005-01-01
As shown by
import datetime
datetime.date(2005, 1, 1)-datetime.timedelta(days=12784)
# datetime.date(1970, 1, 1)
your number is the number of days since 1970-01-01.
So, you can get the date by:
datetime.date(1970, 1, 1) + datetime.timedelta(days=12784)
# datetime.date(2005, 1, 1)
Here you go:
>>> from datetime import date, timedelta
>>> (date(1970,1,1) + timedelta(days=12784)).strftime("%Y-%m-%d")
'2005-01-01'
The numbers shown are the numbers of days since 1st January 1970, which is the origin of Unix time.
They can be converted using for example:
from datetime import datetime, timedelta
n = 13581
print((datetime.utcfromtimestamp(0) + timedelta(n)).strftime("%Y-%m-%d"))
gives:
2007-03-09
Here timedelta
is called with a single argument, being the offset in days. In general it is called with timedelta(days, seconds, microseconds)
but all of these arguments default to zero.
Just in case for a column in dataframe while using the accepted answer, you can use .apply
:
df["column"].apply(lambda x: (datetime.utcfromtimestamp(0) + timedelta(int(x))).strftime("%Y-%m-%d"))