HelloToEarth
HelloToEarth

Reputation: 2127

Combining dataframe year and month into new object Python

I have a dataframe with separated columns of just Year and Month like:

Year        Month
2001        1
2001        2
2001        3
.
.
2010        1
2010        2
.

Converting to pd.datetime using pd.to_datetime(df[['year', 'month']]) requires days to match the format so I get the error:

ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day] is missing

I felt like I could just fill a new column with Day = 1 repeated but I would like to avoid this because I want to create a time series by the Year-Month only.

Is there a way to map Year-Month to a date to graph properly?

Upvotes: 2

Views: 1878

Answers (1)

piRSquared
piRSquared

Reputation: 294298

There is not such thing as a month only datetime thingy.

pd.to_datetime

assign creates a copy of df with the columns as specified in the arguments`.

As @timgeb stated:

Explanation: df.assign(day=1) is a quick way to create a temporary dataframe with a 'day' column without having to modify your original dataframe.

pd.to_datetime(df.assign(day=1))

0   2001-01-01
1   2001-02-01
2   2001-03-01
3   2010-01-01
4   2010-02-01
dtype: datetime64[ns]

to_period

You may want to use to_period.

pd.to_datetime(df.assign(day=1)).dt.to_period('M')

0   2001-01
1   2001-02
2   2001-03
3   2010-01
4   2010-02
dtype: object

Upvotes: 3

Related Questions