pandas 获取季度,月度,年度首尾日期的方法
可实现类似于sql中的dateadd、datesub的功能
两种获取日期的方式
z=datetime.datetime(2016,12,5) z=datetime.datetime.strptime('20150112','%Y%m%d') for x in ['01','02','03','04','05','06','07','08','09','10','11','12']: d='2015'+x+'12' z=datetime.datetime.strptime(d,'%Y%m%d')#当天 z1=z+pd.tseries.offsets.DateOffset(days=1-z.day)#当月第一天 z2=z+pd.tseries.offsets.DateOffset(months=1,days=-z.day)#当月最后一天 x1 = z + pd.tseries.offsets.DateOffset(months=-((z.month - 1) % 3), days=1 - z.day) # 当季第一天 x2 = z + pd.tseries.offsets.DateOffset(months=3 - ((z.month - 1) % 3), days=-z.day) # 当季最后一天 y1 = z + pd.tseries.offsets.DateOffset(months=1 - z.month, days=1 - z.day) # 当年第一天 y2 = z + pd.tseries.offsets.DateOffset(years=1,months=1 - z.month, days= - z.day) # 当年最后一天 print(d,':','月:',z1,z2,'季:',x1,x2) print(y1.strftime("%Y-%m-%d"),y2.strftime("%Y-%m-%d"))
当月最后一天=当月第一天+1月-1天
当季最后一天=当季第一天+3月-1天
当年最后一天=当年第一天+1年-1天
今天前一天:
datetime.datetime.now()+pd.tseries.offsets.DateOffset(days=-1)
今天星期几:
datetime.datetime.now().weekday()
生成时间序列:
start = datetime.datetime.strptime('2018-01-01', '%Y-%m-%d') end = datetime.datetime.strptime('2018-01-08', '%Y-%m-%d') for x in pd.date_range(start,end,freq="3D"): #50min等 print(x.strftime('%Y%m%d'))
相关推荐
三石 2020-10-30
roamer 2020-10-29
三石 2020-10-29
wangquannuaa 2020-10-15
wangquannuaa 2020-09-29
jzlixiao 2020-09-15
wangquannuaa 2020-08-30
三石 2020-08-23
逍遥友 2020-08-21
jzlixiao 2020-08-18
wangquannuaa 2020-08-17
QianYanDai 2020-08-16
cjsyrwt 2020-08-14
jzlixiao 2020-07-29
xirongxudlut 2020-07-20
mmmjyjy 2020-07-16
QianYanDai 2020-07-05
QianYanDai 2020-07-05
june0 2020-07-04