通过之前的文章,大家对pandas都有了基础的了解,在接下来的文章中就是对pandas的一些补充,pandas对日期处理函数。
一、pandas日期功能
1) 创建一个日期范围
通过指定周期和频率来使用date.range()函数,默认频率为/天
# pandas日期处理
import pandas as pd
import numpy as np
# data.range() 创建日期序列
print(pd.date_range('1/1/2011', periods=5))
"""
输出:
DatetimeIndex(['2011-01-01', '2011-01-02',
'2011-01-03', '2011-01-04',
'2011-01-05'],
dtype='datetime64[ns]', freq='D')
"""
2)更改日期频率
# 更改日期频率
# 按月,输出每月的1号的前一天
print(pd.date_range('1/1/2011', periods=5, freq='M'))
"""
输出:
DatetimeIndex(['2011-01-31', '2011-02-28',
'2011-03-31', '2011-04-30',
'2011-05-31'],
dtype='datetime64[ns]', freq='M')
"""
3)商业日期
bdate_range()表示商业日期范围,与date_range()不同,它不包括周六和周天
# bdate_range() 商业日期范围,不包括周六和周天
print(pd.bdate_range('8/2/2019', periods=5))
"""
输出:
DatetimeIndex(['2019-08-02', '2019-08-05',
'2019-08-06', '2019-08-07',
'2019-08-08'],
dtype='datetime64[ns]', freq='B')
"""
date_range()默认的是日历上的日期
import pandas as pd
import numpy as np
start = pd.datetime(2019, 8,2)
end = pd.datetime(2019, 8, 8)
print(pd.date_range(start, end))
"""
输出:
DatetimeIndex(['2019-08-02', '2019-08-03',
'2019-08-04', '2019-08-05',
'2019-08-06', '2019-08-07',
'2019-08-08'],
dtype='datetime64[ns]', freq='D')
"""
二、pandas timedelta
1)通过传递字符串,创建timedelta对象:
import pandas as pd
# 通过传递字符串文字,我们可以创建一个timedelta对象。
print( pd.Timedelta('2 days 9 hours 15 minutes 30 seconds'))
"""
输出:
2 days 09:15:30
"""
2)用单位传递一个智能化素,创建一个timedelta:
# 通过用单位传递一个整数值,一个参数会创建一个Timedelta对象。
print(pd.Timedelta(6, unit='h'))
"""
输出:
0 days 06:00:00
"""
3)数据偏移
"""
数据偏移,诸如 - 周,日,小时,分钟,秒,毫秒,微秒,纳秒等
数据偏移量也可用于构建。
"""
print(pd.Timedelta(days=2))
"""
输出:
2 days 00:00:00
"""
三、python datetime
1) 获取今天的时间:
import datetime
print('the Date tody is:', datetime.datetime.today())
"""
输出:
the Date tody is: 2019-08-07 06:52:15.732195
"""
2)获取当前的年,月,星期
# 当前的年月份,星期
date_today = datetime.date.today()
print(date_today)
print('this year:',date_today.year)
print('this Month:',date_today.month)
print('month name:',date_today.strftime('%B'))
print('this week day:',date_today.day)
print('week day Name:',date_today.strftime('%A'))
"""
输出:
2019-08-07
this year: 2019
this Month: 8
month name: August
this week day: 7
week day Name: Wednesday
"""
3)日期时间算术
# 日期时间算术
day1 = datetime.date(2019, 8, 7)
print('day1:', day1.ctime())
day2 = datetime.date(1995, 5, 12)
print('day2:', day2.ctime())
print('number of days:', day1-day2)
"""
输出:
day1: Wed Aug 7 00:00:00 2019
day2: Fri May 12 00:00:00 1995
number of days: 8853 days, 0:00:00
"""
date_today = datetime.date.today()
no_df_days = datetime.timedelta(days =4)
before_four_days = date_today-no_df_days
print("before four days:", before_four_days)
"""
输出:
before four days: 2019-08-03
"""
after_four_days = date_today + no_df_days
print("after fours days:", after_four_days)
"""
输出:
after fours days: 2019-08-11
"""
4)时间比较
# 时间的比较
date_today = datetime.date.today()
no_of_days = datetime.timedelta(days=4)
before_four_days = date_today-no_df_days
after_four_days = date_today + no_df_days
date1 = datetime.date(2018, 6, 6)
print('date1:', date1)
if date1 == before_four_days:
print('same dates')
if date_today > date1:
print('past Date')
if date1 < after_four_days:
print('future date')
"""
输出:
date1: 2018-06-06
past Date
future date
"""
本文分享自 Python爬虫scrapy 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!