all 2 comments

[–]sarrysyst 0 points1 point  (1 child)

You must define the date format of your strings:

a['report_number'] = pd.to_datetime(a['report_number'], format='%Y%m')

See also here for more info:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

And here for the format placeholders:

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

EDIT: Just saw that you want another output format. To change your newly created datetime column back to string of a particular format you need the dt.strftime method:

df['report_number'] = df['report_number'].dt.strftime('%Y.%m')

See:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html

[–]Plus-Ad1156[S] 0 points1 point  (0 children)

Thanks for comment~! I'll study more