繁体   English   中英

如何使用python numpy或pandas将日期和时间信息添加到时间序列数据

[英]How to add date and time information to time series data using python numpy or 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

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

文档在开始时使用date_range给出了类似的示例。 如果您有一个Series对象,则可以在适当的时间(我假设10132013年的错字)开始创建DatetimeIndex ,其频率为一秒,且长度适当:

>>> 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

如何将python的datetime模块与timeestamps结合使用?

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>的日期时间对象的列表START_TIME + <given interval from time series data>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM