使用petl和Python扩展一个季度的列

时间:2018-06-06 18:53:31

标签: python datetime petl

我有一个季度月表:

Effective Date

3/31/1981
6/30/1981
9/30/1981
12/31/1981
3/31/1982
6/30/1982
9/30/1982
12/31/1982
3/31/1983
6/30/1983

我试图找出如何扩展这些日期并插入剩余的月份,以便获得完整日期列。一直在调查petl,我不确定如何"添加"每个日期一个月,并将其插入不同的位置。

1 个答案:

答案 0 :(得分:0)

你能用pandas和pd.date_range吗?

import pandas as pd

dates = [
    '3/31/1981',
    '6/30/1981',
    '9/30/1981',
    '12/31/1981',
    '3/31/1982',
    '6/30/1982',
    '9/30/1982',
    '12/31/1982',
    '3/31/1983',
    '6/30/1983'
]

s = pd.to_datetime(dates)

r = pd.date_range(start=min(s), end = max(s), freq='M').strftime('%d/%m/%Y').tolist()

print(r)

返回:

['31/03/1981', '30/04/1981', '31/05/1981', '30/06/1981', 
'31/07/1981', '31/08/1981', '30/09/1981', '31/10/1981', 
'30/11/1981', '31/12/1981', '31/01/1982', '28/02/1982', 
'31/03/1982', '30/04/1982', '31/05/1982', '30/06/1982', 
'31/07/1982', '31/08/1982', '30/09/1982', '31/10/1982', 
'30/11/1982', '31/12/1982', '31/01/1983', '28/02/1983', 
'31/03/1983', '30/04/1983', '31/05/1983', '30/06/1983']