我有一份包含开始和结束日期的合同清单。
如何计算合约有效期内重叠合约的数目?
df = pd.DataFrame({
'contract': pd.Series(['A1', 'A2', 'A3', 'A4']),
'start': pd.Series(['01/01/2015', '03/02/2015', '15/01/2015', '10/01/2015']),
'end': pd.Series(['16/01/2015', '10/02/2015', '18/01/2015', '12/01/2015'])
})
给出:
contract end start
0 A1 16/01/2015 01/01/2015
1 A2 10/02/2015 03/02/2015
2 A3 18/01/2015 15/01/2015
3 A4 12/01/2015 10/01/2015
A1与A3和A4重叠,因此重叠= 2。 A2重叠,没有合约,因此重叠= 0。 A3与A1重叠,因此重叠= 1。 A4与A1重叠,因此重叠= 1。
我可以比较每个时间跨度(开始到结束),但那是O(n**2)
还有更好的主意吗?
答案 0 :(得分:2)
这是一种方法:
df = pd.DataFrame({
'contract': pd.Series(['A1', 'A2', 'A3', 'A4']),
'start': pd.Series(['01/01/2015', '03/02/2015', '15/01/2015', '10/01/2015']),
'end': pd.Series(['16/01/2015', '10/02/2015', '18/01/2015', '12/01/2015'])
})
df['start'] = pd.to_datetime(df.start, dayfirst=True)
df['end'] = pd.to_datetime(df.end, dayfirst=True)
periods = df[['start', 'end']].apply(lambda x: (pd.date_range(x['start'], x['end']),), axis=1)
overlap = periods.apply(lambda col: periods.apply(lambda col_: col[0].isin(col_[0]).any()))
df['overlap_count'] = overlap[overlap].apply(lambda x: x.count() - 1, axis=1)
print df
哪个收益率:
contract end start overlap_count
0 A1 2015-01-16 2015-01-01 2
1 A2 2015-02-10 2015-02-03 0
2 A3 2015-01-18 2015-01-15 1
3 A4 2015-01-12 2015-01-10 1
我已更新代码以输出重叠计数,而不是天数重叠。