Python | Pandas DatetimeIndex.day_name ()

day_name | Python Methods and Functions

DatetimeIndex.day_name() Pandas DatetimeIndex.day_name() returns DateTimeIndex day names with the specified locale ... The default is None, in which case names are returned in English.

Syntax: DatetimeIndex.day_name (locale = None)

Parameters:
locale: locale determining the language in which to return the day name

Return: Index of day names

Example # 1: Use DatetimeIndex.day_name () to return the day names for each entry in the DatetimeIndex object. Return day names in French

# import pandas as pd

import pandas as pd

 
# Create DatetimeIndex
# Here "Q" represents the quarterly frequency

didx = pd.DatetimeIndex (start = ' 2018-11-15 09: 45: 10' , freq = ' Q' , periods = 5 )

 
# Print DatetimeIndex

print (didx)

Output:

Now we want to return the day names in French.

# return day names in French

didx.day_name (locale = 'French' )

Output:

As we can see from the output, the function returned an Index object containing titles days in French.

Let's return the name of the days in English

# return day names in English

didx. day_name (locale = 'English' )

Output:

Example # 2: Use DatetimeIndex.day_name () to return titles days for each entry in the DatetimeIndex object. Return day names in German

# import pandas as pd

import pandas as pd

 
# Create DatetimeIndex
# Here "M" represents the monthly frequency

didx = pd.DatetimeIndex (start = ' 2015-03-02' , freq = 'M ' , periods = 5 )

  
# Print DatetimeIndex

print (didx)

Output:

Now we want to return the names of the days in German.

# return day names in German

didx.day_name (locale = 'German' )

Output:

As we can see from the output, the function returned an Index object containing the day names in German.





Python | Pandas DatetimeIndex.day_name (): StackOverflow Questions

Answer #1

If you"d like to have the date in English:

from datetime import date
import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #"Wednesday"

Answer #2

The date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.

Here is a list of the format codes with their directive and meaning.

%a  Locale’s abbreviated weekday name.
%A  Locale’s full weekday name.      
%b  Locale’s abbreviated month name.     
%B  Locale’s full month name.
%c  Locale’s appropriate date and time representation.   
%d  Day of the month as a decimal number [01,31].    
%f  Microsecond as a decimal number [0,999999], zero-padded on the left
%H  Hour (24-hour clock) as a decimal number [00,23].    
%I  Hour (12-hour clock) as a decimal number [01,12].    
%j  Day of the year as a decimal number [001,366].   
%m  Month as a decimal number [01,12].   
%M  Minute as a decimal number [00,59].      
%p  Locale’s equivalent of either AM or PM.
%S  Second as a decimal number [00,61].
%U  Week number of the year (Sunday as the first day of the week)
%w  Weekday as a decimal number [0(Sunday),6].   
%W  Week number of the year (Monday as the first day of the week)
%x  Locale’s appropriate date representation.    
%X  Locale’s appropriate time representation.    
%y  Year without century as a decimal number [00,99].    
%Y  Year with century as a decimal number.   
%z  UTC offset in the form +HHMM or -HHMM.
%Z  Time zone name (empty string if the object is naive).    
%%  A literal "%" character.

This is what we can do with the datetime and time modules in Python

import time
import datetime

print "Time in seconds since the epoch: %s" %time.time()
print "Current date and time: ", datetime.datetime.now()
print "Or like this: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M")

print "Current year: ", datetime.date.today().strftime("%Y")
print "Month of year: ", datetime.date.today().strftime("%B")
print "Week number of the year: ", datetime.date.today().strftime("%W")
print "Weekday of the week: ", datetime.date.today().strftime("%w")
print "Day of year: ", datetime.date.today().strftime("%j")
print "Day of the month : ", datetime.date.today().strftime("%d")
print "Day of week: ", datetime.date.today().strftime("%A")

That will print out something like this:

Time in seconds since the epoch:    1349271346.46
Current date and time:              2012-10-03 15:35:46.461491
Or like this:                       12-10-03-15-35
Current year:                       2012
Month of year:                      October
Week number of the year:            40
Weekday of the week:                3
Day of year:                        277
Day of the month :                  03
Day of week:                        Wednesday

Get Solution for free from DataCamp guru