0

我在使用 pandas 的日期和时间计算时遇到了麻烦。

我认为有一些逻辑可以从特定日期和时间开始自动计算持续时间。但是我还是找不到。

我想知道如何将日期和时间信息添加到 1 秒持续时间的时间序列数据中。

Before:
10
12
13
..
20
21
19
18

After:
1013-01-01 00:00:00 10
1013-01-01 00:00:01 12
1013-01-01 00:00:02 13
..
1013-10-04 12:45:40 20
1013-10-04 12:45:41 21
1013-10-04 12:45:42 19
1013-10-04 12:45:43 18

任何帮助,将不胜感激。先感谢您。

4

2 回答 2

2

文档在开头使用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
于 2013-10-06T14:56:29.040 回答
1

将python的日期时间模块与时间戳结合使用怎么样?

from datetime import datetime

START_TIME = 1381069963.506736
secs = [10, 12, 13, 20, 21, 19, 18]
dates = [datetime.fromtimestamp(START_TIME + sec) for sec in secs]

在此之后,日期列表是日期时间对象的列表START_TIME + <given interval from time series data>

于 2013-10-06T14:36:34.367 回答