我想了解以下内容:给定一个日期(datetime对象),一周中对应的日期是什么?

例如,星期天是第一天,星期一是第二天。。等等

然后如果输入的内容类似于今天的日期。

实例

>>> today = datetime.datetime(2017, 10, 20)
>>> today.get_weekday()  # what I look for

产量可能是6(因为现在是星期五)


当前回答

import datetime
int(datetime.datetime.today().strftime('%w'))+1

这应该会给你一个真实的数字-1=星期天,2=星期一,等等。。。

其他回答

使用Canlendar模块

import calendar
a=calendar.weekday(year,month,day)
days=["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"]
print(days[a])

我们可以帮助熊猫:

import pandas as pd

如上所述,我们有:

datetime(2017, 10, 20)

如果在jupyter笔记本中执行这一行,我们将得到如下输出:

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

使用weekday()和weekday_name:

如果您希望工作日为整数格式,请使用:

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

输出将为:

4

如果你想把它作为一天的名字,比如星期天、星期一、星期五等,你可以使用:

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

输出将为:

'星期五'

如果Pandas数据帧中有日期列,则:

现在假设您有一个panda数据帧,其日期列如下: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]

现在,如果我们想知道工作日的名称,比如星期一、星期二。。等等,我们可以按如下方式使用.weekday_name:

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

输出将是:

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

如果我们希望从“日期”列中得到工作日的整数,那么我们可以使用:

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

输出将如下所示:

0    3
1    4
2    5
3    6
4    0
Name: Dates, dtype: int64

假设您有timeStamp:字符串变量YYYY-MM-DD HH:MM:SS

步骤1:使用blow代码将其转换为dateTime函数。。。

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

步骤2:现在您可以提取所有必需的功能,如下所示,这将为每个字段创建新的列-小时、月、星期、年、日期

df['Hour'] = df['timeStamp'].apply(lambda time: time.hour)
df['Month'] = df['timeStamp'].apply(lambda time: time.month)
df['Day of Week'] = df['timeStamp'].apply(lambda time: time.dayofweek)
df['Year'] = df['timeStamp'].apply(lambda t: t.year)
df['Date'] = df['timeStamp'].apply(lambda t: t.day)

1700/1/1之后的日期不需要导入的解决方案

def weekDay(year, month, day):
    offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
    week   = ['Sunday', 
              'Monday', 
              'Tuesday', 
              'Wednesday', 
              'Thursday',  
              'Friday', 
              'Saturday']
    afterFeb = 1
    if month > 2: afterFeb = 0
    aux = year - 1700 - afterFeb
    # dayOfWeek for 1700/1/1 = 5, Friday
    dayOfWeek  = 5
    # partial sum of days betweem current date and 1700/1/1
    dayOfWeek += (aux + afterFeb) * 365                  
    # leap year correction    
    dayOfWeek += aux / 4 - aux / 100 + (aux + 100) / 400     
    # sum monthly and day offsets
    dayOfWeek += offset[month - 1] + (day - 1)               
    dayOfWeek %= 7
    return dayOfWeek, week[dayOfWeek]

print weekDay(2013, 6, 15) == (6, 'Saturday')
print weekDay(1969, 7, 20) == (0, 'Sunday')
print weekDay(1945, 4, 30) == (1, 'Monday')
print weekDay(1900, 1, 1)  == (1, 'Monday')
print weekDay(1789, 7, 14) == (2, 'Tuesday')
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