简体   繁体   English

select 按日期记录,其中日期是该月上一个工作日的 15 日

[英]select records by date, where date is 15th ot the previous business day of the month

I am trying to select the records that correspond to each 15th of the month, here is the examle of the data:我正在尝试 select 对应于每月 15 日的记录,这是数据的示例:

           date              open               date1          date2
0       2000-01-03 00:00:00  0               2000-01-17 00:00:00    2000-02-15 00:00:00
1       2000-01-07 00:00:00  6084704         2000-01-17 00:00:00    2000-02-15 00:00:00
3       2000-01-12 00:00:00  992482          2000-01-17 00:00:00    2000-02-15 00:00:00
4       2000-01-17 00:00:00  11721104        2000-02-15 00:00:00    2000-03-15 00:00:00
5       2000-01-18 00:00:00  4459200         2000-02-15 00:00:00    2000-03-15 00:00:00
6       2000-01-19 00:00:00 3652363          2000-02-15 00:00:00    2000-03-15 00:00:00
7       2000-01-27 00:00:00 17421705         2000-02-15 00:00:00    2000-03-15 00:00:00
...
43      2000-03-24 00:00:00 13594539         2000-04-17 00:00:00    2000-05-15 00:00:00
44      2000-03-27 00:00:00 5122526          2000-04-17 00:00:00    2000-05-15 00:00:00
45      2000-03-29 00:00:00 3041827          2000-04-17 00:00:00    2000-05-15 00:00:00
46      2000-03-30 00:00:00 4769960          2000-04-17 00:00:00    2000-05-15 00:00:00
48      2000-03-31 00:00:00 15982410         2000-04-17 00:00:00    2000-05-15 00:00:00
49      2000-04-04 00:00:00 3178232          2000-04-17 00:00:00    2000-05-15 00:00:00
51      2000-04-05 00:00:00 15571975         2000-04-17 00:00:00    2000-05-15 00:00:00
52      2000-04-06 00:00:00 2694165          2000-04-17 00:00:00    2000-05-15 00:00:00
53      2000-04-07 00:00:00 536426           2000-04-17 00:00:00    2000-05-15 00:00:00
55      2000-04-10 00:00:00 5523700          2000-04-17 00:00:00    2000-05-15 00:00:00
56      2000-04-11 00:00:00 11212425         2000-04-17 00:00:00    2000-05-15 00:00:00
58      2000-04-12 00:00:00 546223           2000-04-17 00:00:00    2000-05-15 00:00:00
61      2000-04-13 00:00:00 17913960         2000-05-15 00:00:00    2000-06-15 00:00:00
63      2000-04-17 00:00:00 3824185          2000-05-15 00:00:00    2000-06-15 00:00:00
...
1578    2006-01-03 00:00:00 4453005          2006-01-16 00:00:00    2006-02-15 00:00:00
1579    2006-01-04 00:00:00 6557373          2006-01-16 00:00:00    2006-02-15 00:00:00
1580    2006-01-05 00:00:00 2837987          2006-01-16 00:00:00    2006-02-15 00:00:00
...
1634    2006-03-14 00:00:00 31819879         2006-04-17 00:00:00    2006-05-15 00:00:00
1635    2006-03-20 00:00:00 1541321          2006-04-17 00:00:00    2006-05-15 00:00:00
1636    2006-03-21 00:00:00 47047150         2006-04-17 00:00:00    2006-05-15 00:00:00
1638    2006-03-22 00:00:00 6111712          2006-04-17 00:00:00    2006-05-15 00:00:00


Sometimes there is no 15th, since I have a range of business days.有时没有第 15 天,因为我有一系列工作日。 For such case I'd like to choose the record closes to 15th of a month.对于这种情况,我想选择接近一个月 15 日的记录。 So, the expected outcome would be smth like:因此,预期的结果将是:

3       2000-01-12 00:00:00  992482          2000-01-17 00:00:00    2000-02-15 00:00:00
...
61      2000-04-13 00:00:00 17913960         2000-05-15 00:00:00    2000-06-15 00:00:00
...
1634    2006-03-14 00:00:00 31819879         2006-04-17 00:00:00    2006-05-15 00:00:00

I figured out how to find every 15th of the month, that is not a very difficult:我想出了如何找到每个月的 15 号,这不是很困难:

mid_date_mask = df['date'].map(lambda x: x.day) == 15

df[mid_date_mask]

But I cannot understand how to incorporate a step back when there is no 15th in a month.但是当一个月没有第 15 天时,我无法理解如何退后一步。

I would be very grateful for your help!我将非常感谢您的帮助!

You could try if the following works for you:如果以下内容适合您,您可以尝试:

mid_date_idx = (
    (df["date"].dt.day - 15).abs()
    .groupby([df["date"].dt.year, df["date"].dt.month])
    .idxmin()
    .to_list()
)
mid_date_mask = df.index.isin(mid_date_idx)
  • df is your dataframe df是你的 dataframe
  • First bulid a series from column date with the absolute difference of the day and 15 .首先使用日期和15的绝对差异从列date构建一个系列。
  • Then group the result by year-month pairs, use .idxmin() to get the first index with the minimum per group, and collect them in a list mid_date_idx .然后按年-月对对结果进行分组,使用.idxmin()获取每组最小值的第一个索引,并将它们收集到一个列表mid_date_idx中。
  • Build the mid_date_mask by checking if the index is in the list.通过检查索引是否在列表中来构建mid_date_mask

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

相关问题 Django ORM过滤上个月15日到本月15日之间的记录 - Django ORM filter records between last month 15th date to current month 15th date 需要计算从下个月15号到15号的操作总和 - Need to count the total sum of operations from 15th day to 15th day of next month Select 每月的第一天/第十五天加上 python 的前后一天 - Select first/15th day of month plus the day before and after in python 确定在第15天之后会转到下个月的当前月份变量-python 2.7 - Determine current month variable that rolls over to next month after the 15th day- python 2.7 pandas 每月重新抽样第 15 天 - pandas monthly resample 15th day 计算python中每月的第1个或第15个月的时间 - Calculating time until 1st or 15th of the month in python Discord.py 每月15号发一个embed - Discord.py Sending an embed on the 15th every month 如何线性插补从下个月15日开始的每月15号的每月数据 - how to linearly interpolate monthly data from the 15th of each month that rolls over the 15th of the next month 将 dataframe 列转换为带有该月最后一个工作日的日期类型 - Convert dataframe column into date type with last business day of the month 如何设置日期为上个月的最后15天和下个月的前15天? - How to set date to the last 15 days of the previous month and the first 15 days of the next month?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM