pandas 获取季度,月度,年度首尾日期的方法
作者:jingxian 时间:2022-08-16 06:53:06
可实现类似于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'))
来源:https://blog.csdn.net/u013760453/article/details/54574900
标签:pandas,季度,月度,年度,首尾,日期
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
TensorBoard 计算图的可视化实现
2021-12-06 19:39:58
![](https://img.aspxhome.com/file/2023/8/126908_0s.jpg)
利用types增强vscode中js代码提示功能详解
2023-07-15 05:57:08
![](https://img.aspxhome.com/file/2023/1/55981_0s.png)
微软建议的ASP性能优化28条守则(4)
2008-02-26 17:57:00
进一步理解Python中的函数编程
2023-12-07 16:50:16
pandas 使用insert插入一列
2023-03-24 23:51:40
![](https://img.aspxhome.com/file/2023/2/133272_0s.jpg)
python将.ppm格式图片转换成.jpg格式文件的方法
2023-02-16 19:19:54
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】
2021-10-08 09:04:29
![](https://img.aspxhome.com/file/2023/5/89055_0s.png)
详解python-图像处理(映射变换)
2023-12-23 06:43:35
![](https://img.aspxhome.com/file/2023/9/93709_0s.png)
Python+Pika+RabbitMQ环境部署及实现工作队列的实例教程
2021-08-19 13:13:13
![](https://img.aspxhome.com/file/2023/8/123688_0s.png)
详解Python遍历字典的键和值
2021-07-19 23:02:14
![](https://img.aspxhome.com/file/2023/6/71996_0s.png)
Python进程间通信Queue实例解析
2023-11-06 13:50:08
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2021-08-30 04:17:46
python实现将视频按帧读取到自定义目录
2023-07-15 01:24:11
Python+uiautomator2实现手机锁屏解锁功能
2021-05-26 23:04:12
![](https://img.aspxhome.com/file/2023/4/95154_0s.jpg)
详解python的循环
2022-12-29 04:22:52
![](https://img.aspxhome.com/file/2023/3/93813_0s.png)
Python使用tkinter库实现文本显示用户输入功能示例
2023-09-21 11:00:17
![](https://img.aspxhome.com/file/2023/1/105271_0s.gif)
go sync Once实现原理示例解析
2023-07-01 12:21:13
opencv+python实现图像矫正
2022-09-28 09:28:14
![](https://img.aspxhome.com/file/2023/5/78815_0s.jpg)
django xadmin中form_layout添加字段显示方式
2023-08-06 23:59:12
![](https://img.aspxhome.com/file/2023/0/76070_0s.jpg)
关于Python作用域自学总结
2023-11-02 12:19:53
![](https://img.aspxhome.com/file/2023/7/83747_0s.jpg)