简体   繁体   English

需要上个月的数据,加上上个月的 10 天数据和当月的 3 天数据

[英]Need last month's data with additional 10 days of data from previous month and 3 days of data from the current month

I wanted to pull the data for the last month starting from April 1st - April 30 .我想从 4 月 1 日到 4 月 30 日提取上个月的数据。 But additionally also wanted 10 days of data from the previous month (anything after March 21st ) and 3 days from the current month (May 1st - May 3rd) .但还需要上个月的 10 天数据(3 月 21 日之后的任何数据)和当月 3 天的数据(5 月 1 日 - 5 月 3 日)。 I have written the below code which will extract the data for April month but was struggling to extract the data for March and May.我编写了以下代码,它将提取 4 月份的数据,但正在努力提取 3 月和 5 月的数据。 This is the monthly script so wanted to write a code wherein it will look at the current month and then retrieve the data based on the logic mentioned above in future.这是每月的脚本,所以想写一个代码,它会查看当前月份,然后根据上面提到的逻辑检索数据。

Can someone pls guide me on this?有人可以指导我吗?

import datetime
df["Sent_Date"] = pd.to_datetime(df["Sent_Date"])

from datetime import date, timedelta
#strategy_assignemnt.info()
#Create a last_day_of_the_previous_month variable which will read the system date, then  replace it with the day 1 of the current month and set the timedelta which will consider the last month of the last date
last_day_of_the_previous_month = date.today().replace(day = 1)- timedelta(days= 1)
#Create a first_day_of_the_previous_month variable which will read the system date, then  replace it with the day 1 of the current month and set the timedelta which will consider the last month of the last date
first_day_of_the_previous_month = date.today().replace(day = 1) - timedelta(last_day_of_the_previous_month.day)
first_day_of_the_previous_month = pd.to_datetime(first_day_of_the_previous_month).strftime ('%Y-%m-%d')
last_day_of_the_previous_month = pd.to_datetime(last_day_of_the_previous_month).strftime ('%Y-%m-%d')

Used your same calculation to get prev month prev 10 days & current month 3 days使用相同的计算来获得上个月前 10 天和当前月 3 天

import datetime
from datetime import date, timedelta

prev_month_last = date.today().replace(day=1) - timedelta(days=1)
prev_month_first = prev_month_last.replace(day=1)
prev_month_prev_10days_start = prev_month_first - timedelta(days=10)
prev_month_prev_10days_end = prev_month_first - timedelta(days=1)
curr_month_3days_start = date.today().replace(day=1)
curr_month_3days_end = curr_month_3days_start + timedelta(days=2)

print(f'Previous month start date: {prev_month_first} & end {prev_month_last}')
print(f'Ten days before previous month start date: {prev_month_prev_10days_start} & end {prev_month_prev_10days_end}')
print(f'Current month 3days start {curr_month_3days_start} & end {curr_month_3days_end}')

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

相关问题 是否可以通过将当月的天数减少到 1 来从 SQLAlchemy 中的日期时间中提取一个月,以便 plot 图表中的数据? - Is it possible to extract a month from a datetime in SQLAlchemy by reducing the days to 1 in that month in order to plot the data in a graph? 如何在 python 中显示当前月份的前 6 个月数据 - How to display previous 6 months data from current month in python 在数据框中选择每个月的特定天数据 - Select specific days data for each month in a dataframe 从日期列中提取月份中的天数,返回当前月份的日期天数 - extract number of days in month from date column, return days to date in current month “ ValueError:日期超出了月份范围”,因为数据中的所有月份中的所有月份共有31天 - 'ValueError: day is out of range for month' as all the days have 31 days in all month in the data Pyrhon:获取当前月份的月份日期? - Pyrhon: Get days of month present in current 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? 计算从年/月开始到今天的天数 - Count days from beginning of year/month to today 从时间序列数据生成每月的第一个和最后一个工作日 - Generate first and last working day of month from time series data Django过滤当月数据 - Django filter current month data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM