gpt4 book ai didi

python - 在指定的时间戳切片 pandas.Series

转载 作者:行者123 更新时间:2023-11-28 21:33:35 24 4
gpt4 key购买 nike

我正在尝试在指定的时间戳对 pandas.Series 进行切片。从其他 SO 问题中,我得到了以下工作流程:

import pandas as pd
x = ... # some time data
y = ... # some value data

lower_limit_x = pd.to_datetime(x.index) >= pd.to_datetime('2019-01-23 20:59:04')
upper_limit_x = pd.to_datetime(x.index) <= pd.to_datetime('2019-01-23 21:37:44')

lower_limit_y = pd.to_datetime(y.index) >= pd.to_datetime('2019-01-23 20:59:04')
upper_limit_y = pd.to_datetime(y.index) <= pd.to_datetime('2019-01-23 21:37:44')

mask_x = lower_limit_x & upper_limit_x
mask_y = lower_limit_y & upper_limit_y

sliced_x = x[mask_x]
sliced_y = y[mask_y]

但是,如果我从以下数据集开始,该数据集的跨度约为。 2019-01-23 20:45 至 2019-01-23 04:00:

Original data

结果数据似乎是空的。如果我这样做

sliced_y.values

结果为空。

如何按时间戳成功分割数据?

最佳答案

您可以创建单个数据框,然后使用 loc访问者:

df = pd.DataFrame(y.values, index=x.values)

sliced_df = df.loc['2019-01-23 20:59:04': '2019-01-23 21:37:44']

sliced_df 现在是单个数据框,您可以按如下方式访问您的 xy 坐标:

sliced_times = sliced_df.index
sliced_values = sliced_df.iloc[:, 0].values

关于python - 在指定的时间戳切片 pandas.Series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54538727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com