时间处理_pandas_时间处理小结

20220426

https://blog.csdn.net/m0_46128639/article/details/121975381
pandas时间处理函数

20220324

https://mp.weixin.qq.com/s/zd_8LLgJTNx6iav8-yTcEQ
python处理日期时间库汇总

https://blog.csdn.net/eylier/article/details/111145748
阴历阳历转换

https://pypi.org/project/chinesecalendar/
中国节假日识别,不是很准确

20220223
在这里插入图片描述

this_year_begin_date = '2020' + "-" + '52' + "-1"
next_year_begin_date = '2021' + "-" + '0' + "-1"
this_year_begin_date = time.strptime(this_year_begin_date, "%Y-%U-%w")
this_year_begin_date = time.strftime("%Y-%m-%d", this_year_begin_date)
this_year_begin_date

next_year_begin_date = time.strptime(next_year_begin_date, "%Y-%U-%w")
next_year_begin_date = time.strftime("%Y-%m-%d", next_year_begin_date)
next_year_begin_date

当两年交界的一周同时含有两年的天数时候,前一年的最后一周
与下一年的第0周实际上指的同一周

ValueError: day of year out of range
减一周就好了

20211216

from datetime import datetime
today = datetime.now()
day = today.day

20211130

时间周序号对齐包括跨年
见工程化

############


today = pd.to_datetime(run_date)
    this_year_begin_date,this_year_end_date,last_year_begin_date,last_year_end_date = utils.confirm_date_range(today)

def cross_year(this_year_begin_week_num,this_year):
    '''
    :param this_year_begin_week_num:  末尾周序号减周间隔的差值
    :param this_year: 当年年份
    :return: 确定是当年开始周序号
    '''

    this_year_begin_date_confirm = ''
    # 如果取的时间跨年了
    if this_year_begin_week_num < 0:
        begin_week_year = int(this_year) - 1
        begin_week_year = str(begin_week_year)
        max_date = begin_week_year + '-12-31'
        max_date = pd.to_datetime(max_date)
        max_week_num = max_date.isocalendar()[1]
        this_year_begin_week_num = max_week_num - abs(this_year_begin_week_num) 
        this_year_begin_week_num = str(this_year_begin_week_num)
        this_year_begin_date = begin_week_year + "-" + this_year_begin_week_num + "-1"
        this_year_begin_date = time.strptime(this_year_begin_date, "%Y-%U-%w")
        this_year_begin_date = time.strftime("%Y-%m-%d", this_year_begin_date)
        this_year_begin_date_confirm = this_year_begin_date
    else:
        this_year_begin_week = this_year_begin_week_num
        this_year_begin_week = str(this_year_begin_week)
        this_year_begin_date = this_year + "-" + this_year_begin_week + "-1"
        this_year_begin_date = time.strptime(this_year_begin_date, "%Y-%U-%w")
        this_year_begin_date = time.strftime("%Y-%m-%d", this_year_begin_date)
        this_year_begin_date_confirm = this_year_begin_date
    return this_year_begin_date_confirm


def confirm_date_range(today):
    '''
    :param today: 输入的日期
    :return: 返回确认的取数时间范围
    '''
    # 返回上一周的周日日期
    last_sunday_date = utils.check_sunday(today)
    this_year = last_sunday_date.year
    this_year = str(this_year)
    this_year_end_week_num = last_sunday_date.isocalendar()[1]
    this_year_begin_week_num = this_year_end_week_num - 7
    this_year_begin_date = cross_year(this_year_begin_week_num,this_year)

    this_year_end_date = deepcopy(last_sunday_date)

    last_year = int(this_year) -1
    last_year = str(last_year)

    last_year_end_week_num = this_year_end_week_num
    last_year_begin_week_num = this_year_begin_week_num -1
    last_year_begin_date = cross_year(last_year_begin_week_num,last_year)
    last_year_end_week_num = str(last_year_end_week_num)
    last_year_end_date = last_year + "-" + last_year_end_week_num + "-0"
    last_year_end_date = time.strptime(last_year_end_date, "%Y-%U-%w")
    last_year_end_date = time.strftime("%Y-%m-%d", last_year_end_date)
    return (
        this_year_begin_date,
        this_year_end_date,
        last_year_begin_date,
        last_year_end_date,

20211118

today = datetime.now()
today = today.date()
today = pd.to_datetime('2021-11-01')

today = utils.check_monday(today)

this_year = today.year
this_year = str(this_year)
# 检查是否为周一,不是就运行上一个周一

this_year_end_week_num = today.isocalendar()[1] - 1
this_year_begin_week_num = this_year_end_week_num - 6
this_year_week_range = [this_year_begin_week_num,this_year_end_week_num]
this_year_begin_week = this_year_begin_week_num
this_year_begin_week = str(this_year_begin_week)
this_year_begin_date = this_year + '-' + this_year_begin_week + "-1"
this_year_begin_date = time.strptime(this_year_begin_date,'%Y-%U-%w')
this_year_begin_date = time.strftime('%Y-%m-%d',this_year_begin_date)

# 上一个周日
this_year_end_date = utils.get_recent_sunday(today)
this_year_end_date = this_year_end_date.date()

last_year = this_year_end_date.year - 1
last_year = str(last_year)
last_year_end_date = this_year_end_date
last_year_end_date = str(last_year_end_date)
last_year_end_date = last_year_end_date[4:]
last_year_end_date = last_year + last_year_end_date
last_year_end_date = pd.to_datetime(last_year_end_date)

last_year_begin_date = deepcopy(this_year_begin_date)
last_year_begin_date = str(last_year_begin_date)
last_year_begin_date = last_year_begin_date[4:]
last_year_begin_date = last_year + last_year_begin_date
last_year_begin_date = pd.to_datetime(last_year_begin_date)
last_year_begin_date = last_year_begin_date - pd.Timedelta(6,unit='D')

无法直接对年进行加减,需要拼接

https://blog.csdn.net/badassname11/article/details/118160189

%Y 四位数的年份表示(000-9999%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始

通过周序号获得所对应的日期

20211022

https://blog.csdn.net/u014401742/article/details/90592739
时间戳格式化

一、时间datetime

In [10]: pd.datetime(2018,4,1)
Out[10]: datetime.datetime(2018, 4, 1, 0, 0)

二、时间戳Timestamp
表示时间轴上的一个时刻。它提供了方便的时区转换功能。

调用Timestamp()创建任意时间点:

In [98]: pd.Timestamp('2018-08-1 08:02:35')
Out[98]: Timestamp('2018-08-01 08:02:35')

三、时间段Period
Period表示一个标准的时间段。例如某年、某月、某日、某小时等。时间的长短由freq决定。

调用Period()创建任意时间段:

In [100]: pd.Period('2018-08-1 08:02:35',freq='M')
Out[100]: Period('2018-08', 'M')

四、时间间隔Timedetla
通过调用pd.Timedelta()之间创建时间间隔Timedelta对象:

In [102]: pd.Timedelta(weeks=2,days=10,hours=12,minutes=2.4,seconds=10.3)
Out[102]: Timedelta('24 days 12:02:34.300000')

五、时间对象之间的转换
时间戳Timestamp转换为时间段Period

In [104]: pd.Timestamp('2018-08-1 08:02:35').to_period(freq='S')
Out[104]: Period('2018-08-01 08:02:35', 'S')

时间段Period转换为时间戳Timestamp

In [105]: pd.Period('2018-08-1 08:02:35',freq='M').to_timestamp()
Out[105]: Timestamp('2018-08-01 00:00:00')

将两个时间戳Timestamp相减得到时间间隔Timedelta对象

In [106]: pd.Timestamp('2018-08-1 08:02:35')-pd.Timestamp('2018-08-1 07:02:35')
Out[106]: Timedelta('0 days 01:00:00')

六、时间解析
strptime(str)将字符串转换为时间(需要指定格式)

In [11]: value='2018,3,1'
In [12]: pd.strptime(value,'%Y-%m-%d') #将字符串转换为时间
Out[12]: datetime.datetime(2018, 3, 1, 0, 0)

第三方库dateutil.parser的时间解析函数(此时不需要指定格式)

In [14]: from dateutil.parser import parse
In [15]: parse(value)
Out[15]: datetime.datetime(2018, 3, 1, 0, 0)

七、格式定义

格式	说明
%Y	4位数的年
%y	2位数的年
%m	2位数的月[01,12]
%d	2位数的日[01,31]
%H	时(24小时制)[00,23]
%l	时(12小时制)[01,12]
%M	2位数的分[00,59]
%S	秒[00,61]有闰秒的存在
%w	用整数表示的星期几[0(星期天),6]
%F	%Y-%m-%d简写形式例如,2017-06-27
%D	%m/%d/%y简写形式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值