gpt4 book ai didi

Python/Pandas - 如何在 Python/Pandas 中连接 2 个带有日期的数组的最佳实践

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

我是 Python 的新手 - 为了在抵押计划中进行利息的程序计算,我需要使用日期进行计算。有一个用于付款的日期数组,另一个用于总结利息的日期数组。

我完全坚持合并两个日期数组 - 我只需要一个具有排序日期的合并数组。我阅读了不同的方法(数据帧、联接、合并),但找不到任何解决方案。

import pandas as pd
date_range_1 = pd.date_range(start='1/1/2019', periods=12,freq='M')
date_range_2 = pd.date_range(start='31/3/2019',periods=4,freq='3M')

Question-1: How to join these 2 arrays
Question-2: How to sort by date

Output should be:
'1/1/2019'
'1/2/2019'
'1/3/2019'
'31/3/2019'
'1/4/2019'
'1/5/2019'
'1/6/2019'
'30/6/2019'
...

编辑:完整输入+解决方案:

import pandas as pd
dates_1= pd.date_range('2018-01-01','2020-01-01' , freq='1M')-pd.offsets.MonthBegin(1)
dates_2= pd.date_range('2018-03-31','2020-03-31' , freq='3M')-pd.offsets.MonthEnd(0)
Union_dates = pd.Series(sorted(dates_1.union(dates_2)))
print(Union_dates)

最佳答案

IIUC,您需要Index.union :

pd.Series(sorted(date_range_1.union(date_range_2)))
<小时/>
0    2019-01-31
1 2019-02-28
2 2019-03-31
3 2019-04-30
4 2019-05-31
5 2019-06-30
6 2019-07-31
7 2019-08-31
8 2019-09-30
9 2019-10-31
10 2019-11-30
11 2019-12-31
dtype: datetime64[ns]

关于Python/Pandas - 如何在 Python/Pandas 中连接 2 个带有日期的数组的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881928/

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