Get month-day pair without a year from pandas date time

I am trying to use this, but eventually, I get the same year-month-day format where my year changed to default "1900". I want to get only month-day pairs if it is possible.

df['date'] = pd.to_datetime(df['date'], format="%m-%d")

>Solution :

If you transform anything to date time, you’ll always have a year in it, i.e. to_datetime will always yield a date time with a year.

Without a year, you will need to store it as a string, e.g. by running the inverse of your example:

df['date'] = df['date'].dt.strftime(format="%m-%d")

Leave a Reply