Skip to content
Related Articles

Related Articles

Get Day from date in Pandas – Python
  • Last Updated : 10 Jul, 2020

Let’s discuss how to get the day from the date in Pandas. There can be various ways for doing the same. Let’s go through them with the help of examples for better understanding.

Example 1 : Pandas.dt_range takes input as a range of dates and returns a fixed frequency DatetimeIndex. Series.dt.dayofweek returns the day of the week ranging from 0 to 6 where 0 denotes Monday and 6 denotes Sunday.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
date = pd.date_range('2018-12-30', '2019-01-07',
                     freq='D').to_series()
date.dt.dayofweek

chevron_right


Output :







Example 2 : Pandas.DataFrame acts as a dict type container for series objects. pandas.to_datetime converts the input to datetime.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
date = pd.DataFrame({'inputDate':['2020-07-07']})
date['inputDate'] = pd.to_datetime(date['inputDate'])
date['dayOfWeek'] = date['inputDate'].dt.day_name()
  
date

chevron_right


Output :





Example 3 : For more than one input date.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
date = pd.DataFrame({'inputDates':['2015-01-07', '2015-12-02',
                                   '2005-01-03', '2016-11-13',
                                   '2020-06-03'], 
                     'inputVals':[1, 2, 3, 4, 5]})
  
date['inputDates'] = pd.to_datetime(date['inputDates'])
date['dayOfWeek'] = date['inputDates'].dt.day_name()
  
date

chevron_right


Output :

pandas-day-from-date-1



Example 4 : In order to print the days in a particular format.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
date = pd.DataFrame({'inputDates':['1999-7-14', '1998-12-14'
                                   '2001-01-18', '2020-07-18',
                                   '2006-01-8'], 
                     'inputVals':[1, 2, 3, 4, 5]})
  
date['inputDates'] = pd.to_datetime(date['inputDates'])
date['dayOfWeek'] = date['inputDates'].dt.dayofweek
days = {0:'Mon', 1:'Tues', 2:'Wed', 3:'Thurs', 4:'Fri', 5:'Sat', 6:'Sun'}
date['dayOfWeek'] = date['dayOfWeek'].apply(lambda x: days[x])
  
date

chevron_right


Output :

pandas-day-from-date-2

Example 5 : Take input as a range of dates and print their names along with the numbers given(0-6).

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
  
myDate = pd.DataFrame({'inputDates':list(pd.date_range('2018-12-30',
                                                       '2019-01-07'
                                                       freq ='D').to_series())})
  
myDate['inputDates'] = pd.to_datetime(myDate['inputDates'])
myDate['dayOfWeek'] = myDate['inputDates'].dt.dayofweek
myDate['dayOfWeek'] = myDate['inputDates'].dt.day_name()
  
myDate

chevron_right


Output :

python-days-from-date

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :