我有一个熊猫系列,其中时间戳作为索引,而浮点数作为值
pd.Series(
data=[150.0, 151.0, ...],
index=[
datetime.datetime(2020, 1, 24, 15, 53, 54, 532325, tzinfo=None, fold=0),
datetime.datetime(2020, 1, 24, 16, 1, 7, 896288, tzinfo=None, fold=0),
...]
)
导致:
2020-01-24T15:53:54.532325 150.0
2020-01-24T16:01:07.896288 151.0
2020-01-24T16:01:10.862610 152.0
2020-01-24T16:01:13.407573 149.0
2020-01-24T16:33:59.985246 148.0
2020-01-24T16:34:06.411970 150.0
如何通过timedelta w.r.t选择最后一个值及其相应的索引。以秒为单位的最新timedelta值?例如给定的系列和2秒的时间差,我希望该系列的子集
2020-01-24T16:33:59.985246 148.0
2020-01-24T16:34:06.411970 150.0
这应该等同于我应该通过indexing by time使用timestamped_intensity['2020-01-24T16:33:59.9852462':'2020-01-24T16:34:06.411970']
所得到的。
答案 0 :(得分:1)
s.sort_index().tail(2)
#date
#2020-01-24 16:33:59.985246 148.0
#2020-01-24 16:34:06.411970 150.0
#Name: value, dtype: float64
按秒
我们计算索引和索引的时间差,即总和与semnor等于2
n = 2
s[s.sort_index(ascending = False)
.index
.to_series()
.diff().abs()
.dt.total_seconds()
.cumsum()
.fillna(0)
.lt(n)]
答案 1 :(得分:1)
不好,但对我有用:
timedelta_in_seconds = 2
most_recent_timestamp = timestamped_intensity.sort_index().index[-1]
timestamped_intensity[most_recent_timestamp - datetime.timedelta(seconds=timedelta_in_seconds):most_recent_timestamp]