I have a dataframe column in the format of 20180531. I need to split this properly i.e. I can get 2018/05/31. This is a dataframe column that I have and I need to deal with it in a datetime format.
Currently this column is identified as int64 type
CodePudding user response:
I'm not sure how efficient it'll be but if you convert it to a string, and the use pd.to_datetime
with a .format=
..., eg:
df['actual_datetime'] = pd.to_datetime(df['your_column'].astype(str), format='%Y%m%d')
As Emma points out - the astype(str)
is redundant here and just:
df['actual_datetime'] = pd.to_datetime(df['your_column'], format='%Y%m%d')
will work fine.
CodePudding user response:
Assuming the integer dates would always be fixed width at 8 digits, you may try:
df['dt'] = df['dt_int'].astype(str).str.replace(r'(\d{4})(\d{2})(\d{2})', r'\1-\2-\3')