Home > Net >  Split the given integer value as date
Split the given integer value as date

Time:11-04

20160116 Suppose this is the data with datatype integer in a column and now I want to convert it like 2016/01/16 or 2016-01-16 and datatype as date. My column name is system and dataframe is df. How can I do that?

I tried using many date format function but It was not good enough to achieve the answer.

CodePudding user response:

convert using to_datetime, provide the format then convert to the format of your desire

pd.to_datetime(df['dte'], format='%Y%m%d').dt.strftime('%Y/%m/%d')
0    2016/01/06
Name: dte, dtype: object

CodePudding user response:

Using str.replace we can try:

df["date"] = df["system"].astype(str).str.replace(r'(\d{4})(\d{2})(\d{2})', r'\1/\2/\3', regex=True)
  • Related