ShanZhengYang
ShanZhengYang

Reputation: 17631

Pandas pandas.to_datetime(), incorrect parsing

I have a Pandas Dataframe df with lists the Year, Month, and Day separately, with all days of the year. The following is a sample for December:

Year    Month    Day
1995     12      14
1995     12      15
1995     12      15
1995     12      16
1995     12      17

And I wanted to create a timestamp with this data. So, I try this:

df['Datetime'] = pd.to_datetime(df.Year*10000 + df.Month*100 + df.Day, format="%Y%M%d")

Unfortunately, all of the months are listed as January. The following is outputted as this:

1995-01-14 00:01:00
1995-01-15 00:01:00
1995-01-15 00:01:00
1995-01-16 00:01:00
1995-01-17 00:01:00

What is my error above?

Upvotes: 0

Views: 968

Answers (1)

EdChum
EdChum

Reputation: 394041

Your format string is wrong:

"%Y%M%d"

%M means minutes which is why your month defaulted to 1 and you have minutes in your datetimes.

Use:

"%Y%m%d"

See the docs for the correct format specifiers

Upvotes: 1

Related Questions