简体   繁体   English

在Pandas中将通用float64索引转换为Timestamp

[英]Converting a generic float64 index to Timestamp in Pandas

Consider the following series, how can I convert the index of the following series to a Timestamp, assuming that the index is in seconds ? 考虑以下系列,假设索引以单位 ,如何将以下系列的索引转换为时间戳?

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

I tried: 我试过了:

pd.Timestamp(my_series.index)

but I get: 但我得到:

ValueError: Cannot convert Period to Timestamp unambiguously. Use to_timestamp

The final goal is to be able to use pd.resample() to resample from the series above. 最终目标是能够使用pd.resample()从上述系列中重新采样。

Using today's date as the base, you can convert the floats to Timedelta values, interpreting the float values as seconds, and then adding that to today's date: 以今天的日期为基础,可以将浮点数转换为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

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

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