我有一个类似的数据框
df:
date Revenue
0 2000Q1 1517
1 2000Q2 1248
2 2000Q3 1677
3 2000Q4 1393
4 2001Q1 1558
5 2001Q2 1368
6 2001Q3 1790
7 2001Q4 1396
8 2002Q1 1638
9 2002Q2 1507
10 2002Q3 1868
11 2002Q4 1510
df = pd.DataFrame({"date": {0: "2000Q1", 1: "2000Q2", 2: "2000Q3", 3: "2000Q4", 4: "2001Q1", 5: "2001Q2",
6: "2001Q3", 7: "2001Q4", 8: "2002Q1", 9: "2002Q2", 10: "2002Q3",
11: "2002Q4"},
"Revenue": {0: 1517, 1: 1248, 2: 1677, 3: 1393, 4: 1558, 5: 1368, 6: 1790, 7: 1396,
8: 1638, 9: 1507, 10: 1868, 11: 1510}})
我想将这些日期转换为每年四分之一的日期(定期日期格式)。
预期输出:
date Revenue
2000-03-01 1517
2000-06-01 1248
2000-09-01 1677
2000-12-01 1393
2001-03-01 1558
2001-06-01 1368
2001-09-01 1790
2001-12-01 1396
2002-03-01 1638
2002-06-01 1507
2002-09-01 1868
2002-12-01 1510
答案 0 :(得分:4)
我认为您需要转换为datetime
并添加2个月或25个季度:
df['date'] = pd.to_datetime(df['date']) + pd.offsets.MonthOffset(2)
#alternative
#df['date'] = pd.to_datetime(df['date']) + pd.offsets.QuarterBegin(1)
print (df)
date Revenue
0 2000-03-01 1517
1 2000-06-01 1248
2 2000-09-01 1677
3 2000-12-01 1393
4 2001-03-01 1558
5 2001-06-01 1368
6 2001-09-01 1790
7 2001-12-01 1396
8 2002-03-01 1638
9 2002-06-01 1507
10 2002-09-01 1868
11 2002-12-01 1510