Python将查找两个日期列之间的所有周末/日期

Python将查找两个日期列之间的所有周末/日期,python,pandas,date,datetime,Python,Pandas,Date,Datetime,假设我有一个有两列的数据框 Start End 1/1/2015 1/5/2015 1/10/2015 1/12/2015 获取开始日期和结束日期(实际日期,而不是中间的天数)的最佳方法是什么,包括开始日期和结束日期 例如,我会得到2015年1月1日,2015年1月2日,…,2015年1月5日 我想这样做的原因是想知道两次约会之间有多少个周末 下面是示例数据帧和快速解析日期的代码 def lookup(s): """ This is an extremely fas

假设我有一个有两列的数据框

Start      End
1/1/2015  1/5/2015
1/10/2015 1/12/2015
获取开始日期和结束日期(实际日期,而不是中间的天数)的最佳方法是什么,包括开始日期和结束日期

例如,我会得到2015年1月1日,2015年1月2日,…,2015年1月5日

我想这样做的原因是想知道两次约会之间有多少个周末

下面是示例数据帧和快速解析日期的代码

def lookup(s):
    """
    This is an extremely fast approach to datetime parsing.
    For large data, the same dates are often repeated. Rather than
    re-parse these, we store all unique dates, parse them, and
    use a lookup to convert all dates.
    """
    dates = {date:pd.to_datetime(date) for date in s.unique()}
    return s.map(dates)

df = pd.DataFrame({"Start": ["1/1/2015", "1/10/2015"], "End": ["1/5/2015", "1/12/2015"]})
df["Start"] = lookup(df["Start"])
df["End"] = lookup(df["End"])
如果有人知道更好的方法,请让我知道,因为我认为有更好的方法来计算两次约会之间的周末数

我试图理解pd.date_range()函数,并尝试像这样应用它

df["between"] = pd.date_range(df["Start"], df["End"])
但是得到一个错误,说它不能转换输入,我知道我使用这个函数不正确。我想我需要使用apply,但不确定如何将其与此功能一起使用

感谢您的帮助。如果你需要更多的信息,请告诉我


谢谢您的时间。

您可以利用熊猫使用的内置功能。会成为你的朋友吗

# create a dataframe of dates
df = pd.DataFrame({'Dates': pd.date_range("2015-01-01", "2019-08-01")})

# create a series of business days
busines_dates = pd.bdate_range("2015-01-01", "2019-08-30")

# find where the two do not intersect
df.loc[~df['Dates'].isin(busines_dates)]
根据你的问题,我觉得你可能想把它作为一个函数。这是一个基本的问题:

def weekends(start, end):
    df = pd.DataFrame({'Dates': pd.date_range(start, end)})
    busines_dates = pd.bdate_range(start, end)
    answer = df.loc[~df['Dates'].isin(busines_dates)]
    print("There are", answer.shape[0], 'weekends between', start, 'and', end)
    return answer

weekends("2015-01-01", "2019-01-01")


There are 418 weekends between 2015-01-01 and 2019-01-01
          Dates
2    2015-01-03
3    2015-01-04
9    2015-01-10
10   2015-01-11
16   2015-01-17
...         ...
1445 2018-12-16
1451 2018-12-22
1452 2018-12-23
1458 2018-12-29
1459 2018-12-30

您可以利用
pandas
使用的内置功能。会成为你的朋友吗

# create a dataframe of dates
df = pd.DataFrame({'Dates': pd.date_range("2015-01-01", "2019-08-01")})

# create a series of business days
busines_dates = pd.bdate_range("2015-01-01", "2019-08-30")

# find where the two do not intersect
df.loc[~df['Dates'].isin(busines_dates)]
根据你的问题,我觉得你可能想把它作为一个函数。这是一个基本的问题:

def weekends(start, end):
    df = pd.DataFrame({'Dates': pd.date_range(start, end)})
    busines_dates = pd.bdate_range(start, end)
    answer = df.loc[~df['Dates'].isin(busines_dates)]
    print("There are", answer.shape[0], 'weekends between', start, 'and', end)
    return answer

weekends("2015-01-01", "2019-01-01")


There are 418 weekends between 2015-01-01 and 2019-01-01
          Dates
2    2015-01-03
3    2015-01-04
9    2015-01-10
10   2015-01-11
16   2015-01-17
...         ...
1445 2018-12-16
1451 2018-12-22
1452 2018-12-23
1458 2018-12-29
1459 2018-12-30

可能重复使用而不是枚举日期(如果日期相距较远,则效率可能较低)。可能重复使用而不是枚举日期(如果日期相距较远,则效率可能较低)。哇,这太棒了!谢谢,这超出了我的预期,这个功能将非常有用!谢谢哇,这太棒了!谢谢,这超出了我的预期,这个功能将非常有用!谢谢