简体   繁体   English

如何在 python pandas 数据框中将日期范围划分为 365 天的时间段

[英]How to divide a date range into 365 day periods in a python pandas data frame

I have a pandas data frame which contains data that looks like below我有一个 pandas 数据框,其中包含如下所示的数据

import pandas as pd
data = [['0','2018-03-01','2020-04-11'],['1','2017-11-17','2020-11-16'],['2','2017-07-12','2020-07-12']]
df = pd.DataFrame(data,columns=['account_number','contract_start_date','contract_end_date'])
df

在此处输入图像描述

I need to break each account's contract into 365 day periods using the contract_start_date and contract_end_date.我需要使用 contract_start_date 和 contract_end_date 将每个帐户的合同分成 365 天的时间段。 The last period needs to contain whatever days remain.最后一个期间需要包含剩余的任何天数。 An example of that is below下面是一个例子在此处输入图像描述

what I have tried so far: I tried to create a new data frame and thought I could generate the 365 periods using date ranges.到目前为止我尝试过的:我尝试创建一个新的数据框,并认为我可以使用日期范围生成 365 个周期。 It hasn't worked as I hoped.它没有像我希望的那样工作。 Would appreciate any guidance.将不胜感激任何指导。

new_df= pd.concat([pd.DataFrame({'start_date': pd.date_range(row.contract_start_date, row.contract_end_date, freq ='365D'),
 'account_number': row.account_number
                                    }) for row in df.itertuples()], ignore_index=True)
new_df

There are leaps years, so output is different.有闰年,所以output不一样。

Idea is mapped last values by Series.duplicated in new account_number by original contract_end_date and for another values are added 365 days : Idea 由Series.duplicated映射到原始contract_end_date的新account_number中的最后一个值,并为另一个值添加365 days

s1 = pd.to_datetime(df.set_index('account_number')['contract_end_date'])

s2 = new_df['account_number'].map(s1)
s3 = new_df['start_date'] + pd.Timedelta(365, 'd')
mask = new_df['account_number'].duplicated(keep='last')

new_df['contract_end_date'] = np.where(mask, s3, s2)
new_df['days'] = new_df['contract_end_date'] - new_df['start_date']

print (new_df)
   start_date account_number contract_end_date     days
0  2018-03-01              0        2019-03-01 365 days
1  2019-03-01              0        2020-02-29 365 days
2  2020-02-29              0        2020-04-11  42 days
3  2017-11-17              1        2018-11-17 365 days
4  2018-11-17              1        2019-11-17 365 days
5  2019-11-17              1        2020-11-16 365 days
6  2020-11-16              1        2020-11-16   0 days
7  2017-07-12              2        2018-07-12 365 days
8  2018-07-12              2        2019-07-12 365 days
9  2019-07-12              2        2020-07-11 365 days
10 2020-07-11              2        2020-07-12   1 days
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按天对日期进行分组并在 Pandas 数据框或 python 中查找最小值和最大值 - How to group date by day and find min and max value in pandas data frame or python 如何在 Python Pandas 中的数据框中正确更改日期和月份在 position 中更改的日期格式? - How to correctly change format of date where day and month is changes in position in Data Frame in Python Pandas? 如何通过生成两个时间段之间的日期范围来填充数据框中缺少的日期值 - How to fill the missing date values in data frame by generating the range of dates between two time periods 如何使用 Pandas 将日期范围划分为 2 个月的集合? - How to divide a date range into sets of 2 months with Pandas? Pandas 打印数据框列中条件成立的日期周期? - Pandas print date periods where a condition holds in a data frame column? 用列中的日期范围扩展熊猫数据框 - Expanding pandas data frame with date range in columns 分割数据框 Python - Divide Data Frame Python python - 如何在python pandas中分组并取一列的计数除以数据框第二列的唯一计数? - How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas? 如何汇总熊猫特定日期时间的数据? - How to sum up data in specific periods of date time in Pandas? Python pandas date_range 分别分配给每一天的采样 - Python pandas date_range with sampling assigned to each day separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM