作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!