回想起来,MaxU 的答案更好。以下内容可能对旧版 Pandas 有用。
有几种方法可以做到这一点。一种是使用datetime.timedelta 和列表理解:
from datetime import timedelta
import pandas as pd
>>> [pd.to_datetime('01/01/1960') + timedelta(d) for d in [-730,-640,-549,457,-365,275,-184,-92,0,91,182,274,366]]
[Timestamp('1958-01-01 00:00:00'),
Timestamp('1958-04-01 00:00:00'),
Timestamp('1958-07-01 00:00:00'),
Timestamp('1961-04-02 00:00:00'),
Timestamp('1959-01-01 00:00:00'),
Timestamp('1960-10-02 00:00:00'),
Timestamp('1959-07-01 00:00:00'),
Timestamp('1959-10-01 00:00:00'),
Timestamp('1960-01-01 00:00:00'),
Timestamp('1960-04-01 00:00:00'),
Timestamp('1960-07-01 00:00:00'),
Timestamp('1960-10-01 00:00:00'),
Timestamp('1961-01-01 00:00:00')]
所以你可以使用
>>> DataFrame([pd.to_datetime('01/01/1960') + timedelta(d) for d in [-730,-640,-549,457,-365,275,-184,-92,0,91,182,274,366]],columns=['days'])
days
0 1958-01-01
1 1958-04-01
2 1958-07-01
3 1961-04-02
4 1959-01-01
5 1960-10-02
6 1959-07-01
7 1959-10-01
8 1960-01-01
9 1960-04-01
10 1960-07-01
11 1960-10-01
12 1961-01-01