Use strptime
and strftime
value = 20210126
d = datetime.datetime.strptime(str(value), "%Y%m%d")
# datetime.datetime(2021, 1, 26, 0, 0)
d.strftime("%a, %b %m, '%y")
# "Tue, Jan 01, '21"
Format codes listed here!: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
If you are working within Pandas, it may actually be easier to keep these as some simple integer type until you need them, but you can convert the values similarly with its .to_datetime()
method
import pandas as pd
df = pd.DataFrame({"dates": [20010101, 20020202]})
# dates
# 0 20010101
# 1 20020202
pd.to_datetime(df.dates, format="%Y%m%d")
# 0 2001-01-01
# 1 2002-02-02
# Name: dates, dtype: datetime64[ns]
These can also then be represented as strings of some form by using the .dt.strftime
method of the Series
Note .dt
is from the df.dates
column in this case and is a special accessor for datetime Series
df.dates = pd.to_datetime(df.dates, format="%Y%m%d")
# dates
# 0 2001-01-01
# 1 2002-02-02
df.dates.dt.strftime("%a, %b %m, '%y")
# 0 Mon, Jan 01, '01
# 1 Sat, Feb 02, '02
# Name: dates, dtype: object