How do I get the day of week given a date?

前端 未结 26 1420
迷失自我
迷失自我 2020-11-22 04:40

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Mond

相关标签:
26条回答
  • 2020-11-22 05:25

    If you want to generate a column with a range of dates (Date) and generate a column that goes to the first one and assigns the Week Day (Week Day), do the following (I will used the dates ranging from 2008-01-01 to 2020-02-01):

    import pandas as pd
    dr = pd.date_range(start='2008-01-01', end='2020-02-1')
    df = pd.DataFrame()
    df['Date'] = dr
    df['Week Day'] = pd.to_datetime(dr).weekday
    

    The output is the following:

    The Week Day varies from 0 to 6, where 0 corresponds to Monday and 6 to Sunday.

    0 讨论(0)
  • 2020-11-22 05:26

    Assuming you are given the day, month, and year, you could do:

    import datetime
    DayL = ['Mon','Tues','Wednes','Thurs','Fri','Satur','Sun']
    date = DayL[datetime.date(year,month,day).weekday()] + 'day'
    #Set day, month, year to your value
    #Now, date is set as an actual day, not a number from 0 to 6.
    
    print(date)
    
    0 讨论(0)
  • 2020-11-22 05:26

    We can take help of Pandas:

    import pandas as pd
    

    As mentioned above in the problem We have:

    datetime(2017, 10, 20)
    

    If execute this line in the jupyter notebook we have an output like this:

    datetime.datetime(2017, 10, 20, 0, 0)
    

    Using weekday() and weekday_name:

    If you want weekdays in integer number format then use:

    pd.to_datetime(datetime(2017, 10, 20)).weekday()
    

    The output will be:

    4
    

    And if you want it as name of the day like Sunday, Monday, Friday, etc you can use:

    pd.to_datetime(datetime(2017, 10, 20)).weekday_name
    

    The output will be:

    'Friday'

    If having a dates column in Pandas dataframe then:

    Now suppose if you have a pandas dataframe having a date column like this: pdExampleDataFrame['Dates'].head(5)

    0   2010-04-01
    1   2010-04-02
    2   2010-04-03
    3   2010-04-04
    4   2010-04-05
    Name: Dates, dtype: datetime64[ns]
    

    Now If we want to know the name of the weekday like Monday, Tuesday, ..etc we can use .weekday_name as follows:

    pdExampleDataFrame.head(5)['Dates'].dt.weekday_name
    

    the output will be:

    0    Thursday
    1      Friday
    2    Saturday
    3      Sunday
    4      Monday
    Name: Dates, dtype: object
    

    And if we want the integer number of weekday from this Dates column then we can use:

    pdExampleDataFrame.head(5)['Dates'].apply(lambda x: x.weekday())
    

    The output will look like this:

    0    3
    1    4
    2    5
    3    6
    4    0
    Name: Dates, dtype: int64
    
    0 讨论(0)
  • 2020-11-22 05:27

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

    >>> from datetime import datetime
    >>> datetime.today().strftime('%A')
    'Wednesday'
    

    Read more: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

    0 讨论(0)
  • 2020-11-22 05:27

    import numpy as np

    def date(df):

    df['weekday'] = df['date'].dt.day_name()
    
    conditions = [(df['weekday'] == 'Sunday'),
              (df['weekday'] == 'Monday'),
              (df['weekday'] == 'Tuesday'),
              (df['weekday'] == 'Wednesday'),
              (df['weekday'] == 'Thursday'),
              (df['weekday'] == 'Friday'),
              (df['weekday'] == 'Saturday')]
    
    choices = [0, 1, 2, 3, 4, 5, 6]
    
    df['week'] = np.select(conditions, choices)
    
    return df
    
    0 讨论(0)
  • 2020-11-22 05:30

    I solved this for a CodeChef question.

    import datetime
    dt = '21/03/2012'
    day, month, year = (int(x) for x in dt.split('/'))    
    ans = datetime.date(year, month, day)
    print (ans.strftime("%A"))
    
    0 讨论(0)
提交回复
热议问题