Skip to content
Related Articles

Related Articles

Get Day from date in Pandas – Python

Improve Article
Save Article
  • Last Updated : 10 Jul, 2020
Improve Article
Save Article

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.




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

Output :





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




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

Output :





Example 3 : For more than one input date.




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

Output :

pandas-day-from-date-1

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




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

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).




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

Output :

python-days-from-date


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!