How can I separate a sequence of numbers of a series in a dataframe like 20191110
into 2019-11-10
using python and pandas?
It should be a Date format
I tried: df = pd.to_datetime(df.date)
But it gave me a complete wrong date.
It turned 20191028
into: 1970-01-01 00:00:00.020191028
>Solution :
import datetime
datetime.datetime.strptime('20191110', "%Y%m%d").date()
#output
datetime.date(2019, 11, 10)
For a series you can do:
df['A'] = df['A'].dt.strftime('%Y-%m-%d') # Data type of the series will become object(string)
OR
df['A'] = pd.to_datetime(df['A']).dt.date.astype('datetime64[ns]')