Python 通过在Pandas中的另一列中添加新值来扩展Timeindex。

Python 通过在Pandas中的另一列中添加新值来扩展Timeindex。,python,pandas,extend,datetimeindex,Python,Pandas,Extend,Datetimeindex,假设我在Pandas中有以下数据帧: index = pd.date_range(start=pd.Timestamp('2017-07-01'), end=pd.Timestamp('2017-07-01') + pd.Timedelta('10D')) df = pd.DataFrame(data=np.random.rand(11), index=index , columns=['rand']) df.head() rand 2017-07-01 0.794

假设我在Pandas中有以下数据帧:

index = pd.date_range(start=pd.Timestamp('2017-07-01'), end=pd.Timestamp('2017-07-01') + pd.Timedelta('10D'))
df = pd.DataFrame(data=np.random.rand(11), index=index , columns=['rand'])
df.head()

              rand
2017-07-01 0.794164
2017-07-02 0.948194
2017-07-03 0.432187
2017-07-04 0.750968
2017-07-05 0.591830

我想通过将索引列扩展为添加值的数量,即直到2017-07-19为止,向
rand
列添加十个新值。我想知道是否有任何方法可以只通过向
rand
列添加值来自动扩展时间索引。

您可以创建新的
DataFrame
,然后:


您可以首先创建数据帧,然后通过将period设置为数据帧的长度,将frequency设置为1D来设置索引。如果要添加新的“rand”数据,可以使用
pd.concat
然后设置索引,即

df = pd.DataFrame(data=np.random.rand(25), columns=['rand'])
index = pd.date_range(start=pd.Timestamp('2017-07-01'),freq='1D',periods=df.shape[0])
df.set_index(index)
输出:

rand 2017-07-01 0.128300 2017-07-02 0.039629 2017-07-03 0.797066 2017-07-04 0.023662 2017-07-05 0.350117 2017-07-06 0.945745 2017-07-07 0.182427 2017-07-08 0.792960 2017-07-09 0.066210 2017-07-10 0.774758 2017-07-11 0.824564 2017-07-12 0.872008 2017-07-13 0.996188 2017-07-14 0.671798 2017-07-15 0.204903 2017-07-16 0.087394 2017-07-17 0.718709 2017-07-18 0.224255 2017-07-19 0.576668 2017-07-20 0.789603 2017-07-21 0.352212 2017-07-22 0.601235 2017-07-23 0.984145 2017-07-24 0.182860 2017-07-25 0.796244 兰德 2017-07-01 0.128300 2017-07-02 0.039629 2017-07-03 0.797066 2017-07-04 0.023662 2017-07-05 0.350117 2017-07-06 0.945745 2017-07-07 0.182427 2017-07-08 0.792960 2017-07-09 0.066210 2017-07-10 0.774758 2017-07-11 0.824564 2017-07-12 0.872008 2017-07-13 0.996188 2017-07-14 0.671798 2017-07-15 0.204903 2017-07-16 0.087394 2017-07-17 0.718709 2017-07-18 0.224255 2017-07-19 0.576668 2017-07-20 0.789603 2017-07-21 0.352212 2017-07-22 0.601235 2017-07-23 0.984145 2017-07-24 0.182860 2017-07-25 0.796244
创建一个新的数据帧,然后调用
pd.concat
rand 2017-07-01 0.128300 2017-07-02 0.039629 2017-07-03 0.797066 2017-07-04 0.023662 2017-07-05 0.350117 2017-07-06 0.945745 2017-07-07 0.182427 2017-07-08 0.792960 2017-07-09 0.066210 2017-07-10 0.774758 2017-07-11 0.824564 2017-07-12 0.872008 2017-07-13 0.996188 2017-07-14 0.671798 2017-07-15 0.204903 2017-07-16 0.087394 2017-07-17 0.718709 2017-07-18 0.224255 2017-07-19 0.576668 2017-07-20 0.789603 2017-07-21 0.352212 2017-07-22 0.601235 2017-07-23 0.984145 2017-07-24 0.182860 2017-07-25 0.796244