立即注册 登录
气象家园 返回首页

一大碗年糕的个人空间 http://bbs.06climate.com/?116087 [收藏] [复制] [分享] [RSS]

日志

随机时间序列按要求分组

已有 87 次阅读2023-10-27 15:39

生成自ChatGPT:要将一个非等距时间序列分组,同时更改序号。将邻近6小时以内的时间点放在同一组中,你可以使用Python来实现。以下是一个示例代码,假设你有一个时间序列的列表,其中每个元素是一个datetime对象:

from datetime import datetime, timedelta

# 你的时间序列
data = ["2019-05-26 12:30:00", "2019-05-26 00:30:00", "2019-05-18 18:30:00", "2019-04-27 12:30:00", "2018-06-05 00:30:00", "1980-04-19 12:30:00", "1980-04-19 06:30:00", "1980-04-19 00:30:00", "1980-04-18 18:30:00", "1980-04-18 12:30:00"]

# 将字符串转换为datetime对象,并按时间排序
timestamps = [datetime.strptime(ts, "%Y-%m-%d %H:%M:%S") for ts in data]
timestamps.sort()

# 初始化一个字典用于存储分组和对应的序号
groups = {}
group_number = 1

# 定义一个函数来判断两个时间是否在6小时内
def within_six_hours(time1, time2):
    return (time2 - time1) <= timedelta(hours=6)

# 遍历时间序列并分组
current_group = []
for timestamp in timestamps:
    if not current_group:
        current_group.append(timestamp)
    else:
        if within_six_hours(current_group[-1], timestamp):
            current_group.append(timestamp)
        else:
            group_key = current_group[0]
            group_value = current_group
            groups[group_key] = (group_number, group_value)
            current_group = [timestamp]
            group_number += 1

# 处理最后一个分组
if current_group:
    group_key = current_group[0]
    group_value = current_group
    groups[group_key] = (group_number, group_value)

# 打印分组结果
for key, (group_number, value) in groups.items():
    print(f'Group {group_number} starting at {key}:')
    for timestamp in value:
        print(f'  {timestamp}')

import pandas as pd

# 之前生成的字典
groups = {
    datetime(2018, 6, 5, 0, 30): (1, [datetime(2018, 6, 5, 0, 30)]),
    datetime(1980, 4, 18, 12, 30): (2, [datetime(1980, 4, 18, 12, 30)]),
    datetime(1980, 4, 18, 18, 30): (2, [datetime(1980, 4, 18, 18, 30)]),
    # ... 其他分组数据 ...
}

# 创建一个空的DataFrame
df = pd.DataFrame(columns=['Group', 'Timestamp'])

# 遍历字典并将数据添加到DataFrame
for key, (group_number, value) in groups.items():
    for timestamp in value:
        df = df.append({'Group': group_number, 'Timestamp': timestamp}, ignore_index=True)

# 打印DataFrame
print(df)


评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

返回顶部