gpt4 book ai didi

python - 将 Timeseries 的索引从 datetime64[ns] 转换为 datetime64[s] 而不会丢失信息

转载 作者:行者123 更新时间:2023-12-01 07:25:45 25 4
gpt4 key购买 nike

我正在研究一个由时间戳索引的时间序列,精度为 ns,但实际上它应该是每秒一个。我需要在几秒钟内转换此时间戳,因为我想提取一些周期性模式,有时我会丢失数据点,我将在每秒重新采样转换后的数据时间后插入这些数据点。

data = np.array([["2019-07-12 10:39:17.817000+00:00", 45],["2019-07-12 10:39:19.007000+00:00", 45],["2019-07-12 10:39:19.996000+00:00", 40],["2019-07-12 10:39:20.497000+00:00", 1],["2019-07-12 10:39:21.489000+00:00", 10],["2019-07-12 10:39:22.498000+00:00", 3],["2019-07-12 10:39:23.491000+00:00", 5],["2019-07-12 10:39:24.501000+00:00", 15],["2019-07-12 10:39:25.495000+00:00", 8],["2019-07-12 10:39:26.489000+00:00", 3],["2019-07-12 10:39:27.497000+00:00", 90],["2019-07-12 10:39:28.490000+00:00", 4],["2019-07-12 10:39:29.498000+00:00", 37],["2019-07-12 10:39:30.490000+00:00", 55]])
df = pd.DataFrame(data[:, 1], index=data[:, 0], columns=["value"])
df.index=pd.to_datetime(df.index)
print(df.to_string())
value
2019-07-12 10:39:17.817000+00:00 45
2019-07-12 10:39:19.007000+00:00 45
2019-07-12 10:39:19.996000+00:00 40
2019-07-12 10:39:20.497000+00:00 1
2019-07-12 10:39:21.489000+00:00 10
2019-07-12 10:39:22.498000+00:00 3
2019-07-12 10:39:23.491000+00:00 5
2019-07-12 10:39:24.501000+00:00 15
2019-07-12 10:39:25.495000+00:00 8
2019-07-12 10:39:26.489000+00:00 3
2019-07-12 10:39:27.497000+00:00 90
2019-07-12 10:39:28.490000+00:00 4
2019-07-12 10:39:29.498000+00:00 37
2019-07-12 10:39:30.490000+00:00 55

我想要的是以秒为单位转换日期时间,但这样做我有重复的值:

df.index = df.index.round(freq="S")
print(df.to_string())
value
2019-07-12 10:39:18+00:00 45
2019-07-12 10:39:19+00:00 45
2019-07-12 10:39:20+00:00 40
2019-07-12 10:39:20+00:00 1
2019-07-12 10:39:21+00:00 10
2019-07-12 10:39:22+00:00 3
2019-07-12 10:39:23+00:00 5
2019-07-12 10:39:25+00:00 15
2019-07-12 10:39:25+00:00 8
2019-07-12 10:39:26+00:00 3
2019-07-12 10:39:27+00:00 90
2019-07-12 10:39:28+00:00 4
2019-07-12 10:39:29+00:00 37
2019-07-12 10:39:30+00:00 55

即使我选择地板而不是圆形,它也不会工作,因为这些值:

2019-07-12 10:39:19.007000+00:00
2019-07-12 10:39:19.996000+00:00

有没有办法以秒为单位转换日期时间,这样就不会创建重复的值?

预期输出:

                          value
2019-07-12 10:39:17+00:00 45
2019-07-12 10:39:18+00:00 45
2019-07-12 10:39:19+00:00 40
2019-07-12 10:39:20+00:00 1
2019-07-12 10:39:21+00:00 10
2019-07-12 10:39:22+00:00 3
2019-07-12 10:39:23+00:00 5
2019-07-12 10:39:24+00:00 15
2019-07-12 10:39:25+00:00 8
2019-07-12 10:39:26+00:00 3
2019-07-12 10:39:27+00:00 90
2019-07-12 10:39:28+00:00 4
2019-07-12 10:39:29+00:00 37
2019-07-12 10:39:30+00:00 55

最佳答案

如果您首先舍入到最接近的 100ms,然后使用 ceil 舍入到最接近的秒,您将得到所需的输出。

import pandas as pd

df = pd.read_csv('something.csv')

df['time'] = pd.to_datetime(df['time'], infer_datetime_format=True)
print(df)

df['time'] = df['time'].dt.round('100ms')
df['time'] = df['time'].dt.ceil('1s')
print(df)

输出:

0  2019-07-12 10:39:18+00:00     45
1 2019-07-12 10:39:19+00:00 45
2 2019-07-12 10:39:20+00:00 40
3 2019-07-12 10:39:21+00:00 1
4 2019-07-12 10:39:22+00:00 10
5 2019-07-12 10:39:23+00:00 3
6 2019-07-12 10:39:24+00:00 5
7 2019-07-12 10:39:25+00:00 15
8 2019-07-12 10:39:26+00:00 8
9 2019-07-12 10:39:27+00:00 3
10 2019-07-12 10:39:28+00:00 90
11 2019-07-12 10:39:29+00:00 4
12 2019-07-12 10:39:30+00:00 37
13 2019-07-12 10:39:31+00:00 55

关于python - 将 Timeseries 的索引从 datetime64[ns] 转换为 datetime64[s] 而不会丢失信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57460871/

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