Swaggy A
Swaggy A

Reputation: 35

Pandas: Convert datetime series to an int by month

I have a dataframe that contains a series of dates, e.g.:

0    2014-06-17
1    2014-05-05
2    2014-01-07
3    2014-06-29
4    2014-03-15
5    2014-06-06
7    2014-01-29

What I would like to do is convert these dates to integers by the month, since all the values are within the same year. So I would like to get something like this:

0    6
1    5
2    1
3    6
4    3
5    6
7    1

Is there a quick way to do this with Pandas?

EDIT: Answered by jezrael. Thanks so much!

Upvotes: 1

Views: 1009

Answers (1)

jezrael
jezrael

Reputation: 862481

Use function dt.month:

print (df)
       Dates
0 2014-06-17
1 2014-05-05
2 2014-01-07
3 2014-06-29
4 2014-03-15
5 2014-06-06
6 2014-01-29


print (df.Dates.dt.month)
0    6
1    5
2    1
3    6
4    3
5    6
6    1
Name: Dates, dtype: int64

Upvotes: 2

Related Questions