It is a memo of the processing of the system that you do not use often but you have to google when you want to change the character string 202003
to 2020-03
of date.
Suppose you have the following character string data called df, and you want to change it to the datetime module of the year and month.
Year / month |
---|
202001 |
202002 |
202003 |
df["Year / month"] = pd.to_datetime(df["Year / month"], format='%Y%m').dt.to_period('M')
Now you can put -
and it will be a date object.
format ='% Y% m'
reads the string as a date module, and laterdt.to_period ('M')
specifies only the year and month.
Year / month |
---|
2020-01 |
2020-02 |
2020-03 |
dt.to_period ('M')
With the same data, try removing the last dt.to_period ('M')
.
Year / month |
---|
202001 |
202002 |
202003 |
df["Year / month"] = pd.to_datetime(df["Year / month"], format='%Y%m')
One day will be added.
Year / month |
---|
2020-01-01 |
2020-02-01 |
2020-03-01 |
Recommended Posts