extract month year from date pandas code example
Example 1: extract year from datetime pandas
df['date'] = pd.to_datetime(df['date'],format='%Y%m%d')
df['year'] = pd.DatetimeIndex(df['date']).year
df['month'] = pd.DatetimeIndex(df['date']).month
Example 2: to extract out only year month from a date column in pandas
df['month_year'] = df['date_column'].dt.to_period('M')