该文档在开头使用date_range
. 如果你有一个Series
对象,你可以DatetimeIndex
在适当的时间开始(我假设1013
是 的错字2013
),频率为一秒,长度适当:
>>> x = pd.Series(np.random.randint(8,24,23892344)) # make some random data
>>> when = pd.date_range(start=pd.datetime(2013,1,1),freq='S',periods=len(x))
>>> when
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01 00:00:00, ..., 2013-10-04 12:45:43]
Length: 23892344, Freq: S, Timezone: None
然后我们可以使用这个作为新索引从原始数据中创建一个新系列:
>>> x_with_time = pd.Series(x.values, index=when)
>>> x_with_time
2013-01-01 00:00:00 13
2013-01-01 00:00:01 14
2013-01-01 00:00:02 15
2013-01-01 00:00:03 22
2013-01-01 00:00:04 16
[...]
2013-10-04 12:45:41 21
2013-10-04 12:45:42 16
2013-10-04 12:45:43 15
Freq: S, Length: 23892344