Home > Software engineering >  How to select month in a pandas date column?
How to select month in a pandas date column?

Time:11-29

I don't know how I can use/extract the month in the highpoint_date column. I don't understand how to use datetime to have only this information. How can I do this?

I just manually made a new column (last line of code) but I think there must be a quicker and easier technique.

Code:

import pandas as pd
expeditions = pd.read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/expeditions.csv")
print(expeditions)

expeditions['month_highpoint'] = expeditions['highpoint_date'].str[-5:-3]

CodePudding user response:

First convert to_datetime and then access various date information using the dt accessor.

For example to access the month, use dt.month:

expeditions['highpoint_date'] = pd.to_datetime(expeditions['highpoint_date'])

expeditions['highpoint_date'].dt.month

# 0         5.0
# 1        10.0
# 2         5.0
#          ... 
# 10361     5.0
# 10362     4.0
# 10363     4.0
# Name: highpoint_date, Length: 10364, dtype: float64

Note that it's also possible to convert highpoint_date into a datetime column during read_csv by using the parse_dates param:

expeditions = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/expeditions.csv',
    parse_dates=['highpoint_date'])
  • Related