简体   繁体   English

pandas 每月重新抽样第 15 天

[英]pandas monthly resample 15th day

I am trying to resample to monthly values but with respect to 15th day我正在尝试重新采样到每月值,但关于第 15 天

I checked the timeseries offsets documentation but there is only我检查了时间序列偏移文档,但只有

M month end frequency SM semi-month end frequency (15th and end of month) MS month start frequency SMS semi-month start frequency (1st and 15th) M 月末频率 SM 半月末频率(15 日和月末) MS 月开始频率 SMS 半月开始频率(1 日和 15 日)

while I need just the 15th day而我只需要第15天

Something like就像是

2000-01-15 8.7
2000-02-15 6.9
2000-03-15 15.8
2000-04-15 12.4

I tried with pd.offsets.MonthBegin and MonthOffset with no results我尝试使用 pd.offsets.MonthBegin 和 MonthOffset 没有结果

Aggregate by starts of months MS and then adjust the resampled time labels by loffset parameter: MS月份开始前进行汇总,然后通过loffset参数调整重新采样的时间标签:

df1 = df.resample('MS', loffset=pd.Timedelta(14, 'd')).sum()

Sample: 样品:

rng = pd.date_range('2017-04-03', periods=15, freq='5D')
df = pd.DataFrame({'a': range(15)}, index=rng)  
print (df)
             a
2017-04-03   0
2017-04-08   1
2017-04-13   2
2017-04-18   3
2017-04-23   4
2017-04-28   5
2017-05-03   6
2017-05-08   7
2017-05-13   8
2017-05-18   9
2017-05-23  10
2017-05-28  11
2017-06-02  12
2017-06-07  13
2017-06-12  14

df1 = df.resample('MS', loffset=pd.Timedelta(14, 'd')).sum()
print (df1)
             a
2017-04-15  15
2017-05-15  51
2017-06-15  39

df1 = df.resample('SMS').sum()
print (df1)
             a
2017-04-01   3
2017-04-15  12
2017-05-01  21
2017-05-15  30
2017-06-01  39

The other answer is deprecated in pandas 1.4.2 and comes with a warning FutureWarning: 'loffset' in.resample() and in Grouper() is deprecated.另一个答案在 pandas 1.4.2中被弃用,并带有警告FutureWarning: 'loffset' in.resample() and in Grouper() is deprecated.

The recommended alternative is do first resample normally, then add a Timedelta to the index:推荐的替代方法是先正常重新采样,然后将Timedelta添加到索引中:

df1 = df.resample('MS').sum()
df1.index += pd.Timedelta(14, 'd')

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

相关问题 需要计算从下个月15号到15号的操作总和 - Need to count the total sum of operations from 15th day to 15th day of next month 如何线性插补从下个月15日开始的每月15号的每月数据 - how to linearly interpolate monthly data from the 15th of each month that rolls over the 15th of the next month Select 每月的第一天/第十五天加上 python 的前后一天 - Select first/15th day of month plus the day before and after in python select 按日期记录,其中日期是该月上一个工作日的 15 日 - select records by date, where date is 15th ot the previous business day of the month 确定在第15天之后会转到下个月的当前月份变量-python 2.7 - Determine current month variable that rolls over to next month after the 15th day- python 2.7 计算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 Django ORM过滤上个月15日到本月15日之间的记录 - Django ORM filter records between last month 15th date to current month 15th date 使用 Pandas 将每日数据重新采样为每月(日期格式) - Resample Daily Data to Monthly with Pandas (date formatting) Pandas 每月重新采样保持第一次日期 - Pandas monthly resample maintaining first date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM