考虑以下系列,我如何将以下系列的索引转换为时间戳,假设索引是秒?
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
我试过了:
pd.Timestamp(my_series.index)
但我明白了:
ValueError: Cannot convert Period to Timestamp unambiguously. Use to_timestamp
最终目标是能够使用pd.resample()
从上述系列中重新取样。
答案 0 :(得分:2)
使用今天的日期作为基础,您可以将浮点值转换为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