How do you extract the month and year from a date in python?

You can directly access the year and month attributes, or request a datetime.datetime:

In [15]: t = pandas.tslib.Timestamp.now()

In [16]: t
Out[16]: Timestamp('2014-08-05 14:49:39.643701', tz=None)

In [17]: t.to_pydatetime() #datetime method is deprecated
Out[17]: datetime.datetime(2014, 8, 5, 14, 49, 39, 643701)

In [18]: t.day
Out[18]: 5

In [19]: t.month
Out[19]: 8

In [20]: t.year
Out[20]: 2014

One way to combine year and month is to make an integer encoding them, such as: 201408 for August, 2014. Along a whole column, you could do this as:

df['YearMonth'] = df['ArrivalDate'].map(lambda x: 100*x.year + x.month)

or many variants thereof.

I'm not a big fan of doing this, though, since it makes date alignment and arithmetic painful later and especially painful for others who come upon your code or data without this same convention. A better way is to choose a day-of-month convention, such as final non-US-holiday weekday, or first day, etc., and leave the data in a date/time format with the chosen date convention.

The calendar module is useful for obtaining the number value of certain days such as the final weekday. Then you could do something like:

import calendar
import datetime
df['AdjustedDateToEndOfMonth'] = df['ArrivalDate'].map(
    lambda x: datetime.datetime(
        x.year,
        x.month,
        max(calendar.monthcalendar(x.year, x.month)[-1][:5])
    )
)

If you happen to be looking for a way to solve the simpler problem of just formatting the datetime column into some stringified representation, for that you can just make use of the strftime function from the datetime.datetime class, like this:

In [5]: df
Out[5]: 
            date_time
0 2014-10-17 22:00:03

In [6]: df.date_time
Out[6]: 
0   2014-10-17 22:00:03
Name: date_time, dtype: datetime64[ns]

In [7]: df.date_time.map(lambda x: x.strftime('%Y-%m-%d'))
Out[7]: 
0    2014-10-17
Name: date_time, dtype: object

In this short guide, I'll show you how to extract Month and Year from a DateTime column in Pandas DataFrame. You can also find how to convert string data to a DateTime. So at the end you will get:

01/08/2021 -> 2021-08
DD/MM/YYYY -> YYYY-MM

or any other date format. We will also cover MM/YYYY.

To start, here is the syntax that you may apply in order extract concatenation of year and month:

.dt.to_period('M')

In the next section, I'll review the steps to apply the above syntax in practice.

Step 1: Create a DataFrame with Datetime values

Lets create a DataFrame which has a single column StartDate:

dates = ['2021-08-01', '2021-08-02', '2021-08-03']
df = pd.DataFrame({'StartDate': dates})

result:

StartDate
2021-08-01
2021-08-02
2021-08-03

In order to convert string to Datetime column we are going to use:

df['StartDate'] = pd.to_datetime(df['StartDate'])

Step 2: Extract Year and Month with .dt.to_period('M') - format YYYY-MM

In order to extract from a full date only the year plus the month: 2021-08-01 -> 2021-08 we need just this line:

df['StartDate'].dt.to_period('M')

result:

0    2021-08
1    2021-08
2    2021-08

Step 3: Extract Year and Month other formats MM/YYYY

What if you like to get the month first and then the year? In this case we will use .dt.strftime in order to produce a column with format: MM/YYYY or any other format.

df['StartDate'].dt.strftime('%m/%Y')
0    08/2021
1    08/2021
2    08/2021

Note: have in mind that this solution might be really slow in case of a huge DataFrame.

Step 4: Extracting Year and Month separately and combine them

A bit faster solution than step 3 plus a trace of the month and year info will be:

  • extract month and date to separate columns
  • combine both columns into a single one
df['yyyy'] = pd.to_datetime(df['StartDate']).dt.year
df['mm'] = pd.to_datetime(df['StartDate']).dt.month
StartDateyyyymm
2021-08-01 2021 8
2021-08-02 2021 8
2021-08-03 2021 8

and then:

df['yyyy'].astype(str) + '-'+ df['mm'].astype(str)

Note: If you don't need extra columns you can just do:

df['StartDate'].dt.year.astype(str) + "-" + df['StartDate'].dt.month.astype(str)

Notebook with all examples: Extract Month and Year from DateTime column

How do you extract the month and year from a date in python?

How do I extract the month and year from a date?

Use the following formula involving the TEXT function to extract the month and year from a date:.
=TEXT(B3,"mmm/yy").
=MONTH(B3).
=YEAR(B3).
=CONCAT(C3,"/",D3).

How do I separate month and year in Python?

“separate year and month from date in python” Code Answer's.
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..

How do I get the month number from a date in python?

“how to extract month from date in python” Code Answer.
import datetime..
date = '2021-05-21 11:22:03'.
datem = datetime. datetime. strptime(date, "%Y-%m-%d %H:%M:%S").
print(datem. day) # 25..
print(datem. month) # 5..
print(datem. year) # 2021..
print(datem. hour) # 11..
print(datem. minute) # 22..

How do I extract month from pandas?

How to Extract Month and Year from DateTime column in Pandas.
Step 1: Create a DataFrame with Datetime values. ... .
Step 2: Extract Year and Month with .dt.to_period('M') - format YYYY-MM. ... .
Step 3: Extract Year and Month other formats MM/YYYY. ... .
Step 4: Extracting Year and Month separately and combine them..