Python中日期范围的并集

2024-06-28 19:50:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python代码,其中我模拟了给定地面段的卫星接触持续时间和日期范围,现在我有了星座中多个(>;=2)卫星的这些数据帧。到目前为止,我通过熊猫分析了每个卫星的数据。 我试图实现的是将多个卫星的重叠日期范围合并到一个结果文件中。以两颗卫星为例:

文件1:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
450.61717646411466  2022-01-01 13:11:18.686564  2022-01-01 13:18:49.303741
272.9796195817538   2022-01-01 14:45:04.846243  2022-01-01 14:49:37.825862

文件2:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
576.600683837155    2022-01-01 13:06:51.364924  2022-01-01 13:16:27.965608
568.5843137051123   2022-01-01 14:40:38.840363  2022-01-01 14:50:07.424677

我的目标是将这些日期范围和持续时间合并并固定在一个文件中,如下所示:

Duration (s)    Start Time (UTC)    Stop Time (UTC)
718.600683837155    2022-01-01 13:06:51.364924  2022-01-01 13:18:49.303741
568.5843137051123   2022-01-01 14:40:38.840363  2022-01-01 14:50:07.424677

pandas(或任何其他库)是否具有处理此类问题的现成功能?否则,有人能帮我弄清楚吗

非常感谢


Tags: 文件数据代码gt功能目标pandastime
1条回答
网友
1楼 · 发布于 2024-06-28 19:50:30

谢谢@MrFuppes和@NuLo的反馈

我最终能够自己进行排序:首先,我将所有输入收集到一个文件中,并根据初始数据对其进行排序。然后第二步是合并它们。下面是我的一段代码,它是一个现在可以工作的函数,我认为它将来可以帮助其他人:

import os
import pandas as pd
def mergeContactsIntoConstellation(originDIR, destinyDIR, station, noSatellites):
'''
Reads each of the individual satellites accesses for a given ground station,
and merge them into a single constellation contact.
:originDIR: directory containing the individual files
:destintyR: directory where the merged file is saved
:station: ground station of interest
:noSatellites: number of satellites in the constellation
'''
iDates = []
fDates = []
for i in range(0, noSatellites):
    file = originDIR + station.name+'_sat{}_Contacts.txt'.format(i+1)
    if os.stat(file).st_size == 0:
        continue
    file = pd.read_csv(originDIR + station.name+'_sat{}_Contacts.txt'.format(i+1),delimiter='\t',header=0,engine='python')
    file.columns = ['duration', 'itime', 'ftime']
    itime = file['itime']
    ftime = file['ftime']

    for iDate, fDate in zip(itime, ftime):
        iDates.append(datetime.strptime(iDate, '%Y-%m-%d %H:%M:%S.%f'))
        fDates.append(datetime.strptime(fDate, '%Y-%m-%d %H:%M:%S.%f'))

newiDates = sorted(iDates)
newfDates = []
for newiDate in newiDates:
    for idate, fdate in zip(iDates, fDates):
        if newiDate == idate:
            newfDates.append(fdate)
Dates = [newiDates, newfDates]

resultingiDates = []
resultingfDates = []
for i in range(0, len(Dates[0])-1):
    if Dates[1][0] >= Dates[0][i+1]:
        Dates[0][0] = min(Dates[0][0], Dates[0][i+1])
        Dates[1][0] = max(Dates[1][0], Dates[1][i+1])
    else:
        resultingiDates.append(Dates[0][0])
        resultingfDates.append(Dates[1][0])
        Dates[0][0] = Dates[0][i+1]
        Dates[1][0] = Dates[1][i+1]
else:
    resultingiDates.append(Dates[0][0])
    resultingfDates.append(Dates[1][0])

jointContacts = pd.DataFrame()
for idate, fdate in zip(resultingiDates, resultingfDates):
    jointContacts=jointContacts.append({
        "Duration (s)": datetime_to_absolutedate(fdate).durationFrom(datetime_to_absolutedate(idate)),
        "Start Time (UTC)": idate,
        "Stop Time (UTC)":fdate}, ignore_index=True)
jointContacts.to_csv(destinyDIR+station.name+'_JointContacts.txt', sep='\t', index=False)

一些函数,如“durationFrom”来自OREKIT的python包装

相关问题 更多 >