python - Python中日期范围的联合
问题描述
我正在编写一个 python 代码,在其中模拟给定地面段的卫星接触持续时间和日期范围,现在我拥有星座中多个 (>=2) 卫星的这些数据帧。到目前为止,我通过 pandas 分析了每颗卫星的数据。我想要实现的是将多个卫星的重叠日期范围合并到一个结果文件中。以两颗卫星为例:
文件 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(或任何其他库)是否有现成的功能来处理这类问题?否则,谁能帮我解决这个问题?
提前谢谢了。
解决方案
感谢@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 包装器。
推荐阅读
- java - 如何在 Java 中为 TreeMap 编写自定义比较器?
- javascript - 如何让 2d 字符在 JavaScript 中运行和跳跃
- ruby-on-rails - 遵循教程时不会显示 Link_to “Read Me”
- r - How to pass an input parameter for a UDF to sapply
- java - 错误 org.hibernate.exception.SQLGrammarException:无法提取 ResultSet
- javascript - WebRTC 通过 Web Audio API 在谷歌 Chrome 上静默
- sql - 在数据框 to_sql 上创建列索引
- typescript - 为什么(何时?) event.target 可以为空?
- php - 响应式菜单在主页中快速关闭
- python - Python 的结构体和处理器的对齐方式