在Pandas中将泛型float64索引转换为时间戳

2024-10-02 10:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑下面的系列,假设索引在内,我如何将下面序列的索引转换为时间戳?在

quantity
1631.083        -5000
1632.395        -5000
1635.482        -5000
1638.536        -1800
1640.818        -5000
1644.739        -5000
1644.828        -5000
1655.214        -1800
1658.691        -4300
1662.751        -5000
1693.350        -5000
Length: 87575, dtype: float64

我试过了:

^{pr2}$

但我得到:

ValueError: Cannot convert Period to Timestamp unambiguously. Use to_timestamp

最终目标是能够使用^{}从上面的系列中重新取样。在


Tags: toconvertuse时间序列lengthtimestampquantity
1条回答
网友
1楼 · 发布于 2024-10-02 10:27:34

以今天的日期为基数,可以将浮点值转换为Timedelta值,将浮点值解释为秒,然后将其添加到今天的日期:

In [15]: s2 = s.copy()

In [16]: s2.index = pd.Timestamp(datetime.date.today()) + pd.TimedeltaIndex(s.index, unit='s')

In [17]: s2
Out[17]: 
2014-12-30 00:27:11.083000   -5000
2014-12-30 00:27:12.395000   -5000
2014-12-30 00:27:15.482000   -5000
2014-12-30 00:27:18.536000   -1800
2014-12-30 00:27:20.818000   -5000
2014-12-30 00:27:24.739000   -5000
2014-12-30 00:27:24.828000   -5000
2014-12-30 00:27:35.214000   -1800
2014-12-30 00:27:38.691000   -4300
2014-12-30 00:27:42.751000   -5000
2014-12-30 00:28:13.350000   -5000
Name: quantity, dtype: int64

相关问题 更多 >

    热门问题