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

标签 python pandas dataframe datetime time-series

我正在研究一个由时间戳索引的时间序列,精度为 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/

相关文章:

python - “RuntimeError: generator raised StopIteration” 如何解决这个Python问题?

python - 计算一个元素在大型数据集中所有过去出现的次数

python - 如何按 '/' 拆分字符串并通过数据帧中的拆分子字符串对其进行重组?

python - Pandas 按三列分组,但保留所有其他列

python - 将 python 字典转换为数据框,其中字典值(列表)作为列,如果该列在字典列表中,则为 1,0

python - Python 中的 IRC 客户端;不是 IRC 机器人

python - Django 模型中 int() 的文字无效

python - 将 nosetests 的覆盖范围限制为仅运行的测试

python - 合并多个文件并通过附加文件名保存它们

python - 为什么 Pandas Series.isin 适用于字符串而不适用于数字?