美文网首页
Python的时间转换

Python的时间转换

作者: 耳朵和爪子 | 来源:发表于2017-06-07 21:06 被阅读1394次

Python 的 Time Series 函数在日常数据分析中是最常应用的。任何一笔订单交易的情况,都可以转化成在时间维度的数据分布,通过这些数据点的分布,可以洞察在数据背后的用户行为特征。根据具体应用场景,Time Series一般分三类。

  1. Timestamps,某一个具体的时间点。
  2. Fixed periods, 某一段时间段,比如 the month January 2007 or the full year
  3. 任意的时间段,给定一个开始一个结束的时间点即可。

Date and Time

Pandas中的date和time时间序列类型,其实就是以Timestamps 为index的Series.

Paste_Image.png
from datetime import datetime,timedelta 
now = datetime.now()  #基本格式为 2017-06-07 10:03:06.994394
print(now)
print(now.year,now.month,now.day) # year,month,day 都可以作为一个index

delta = datetime(2011,1,7) - datetime(2008,6,24,8,15)
print(delta)                             ##output is 926 days, 15:45:00
print(delta.days,delta.seconds)

start = datetime(2011,1,7)
print(start + timedelta(12))  #加12天
print(start - 2*timedelta(12))#减12天

Datetime 和 string 格式的互换

1.time => str

strftime 改变时间格式,但每次需要指定格式

stamp = datetime(2011,1,3)
print(stamp)
print(stamp.strftime('%Y/%m/%d'))
2.Str => time

datetime.strptime 指定形式进行转变

datetime.strptime('2011-01-03','%Y-%m-%d')

parse 将任意时间格式,改为标准series.

from dateutil.parser import parse
print(parse('Feb 28,2016, 3pm'))

日期作为index

date=s['op_time']
print(date)
date1=pd.date_range('2017/4/1','2017/5/31') #data generation
t=list(s.gmv) #必须将series格式转换成list格式
ts=Series(t,index=date1)  #将date1设为 index
print(ts)
print(type(ts))           #查看ts的类型
print(ts[ts.index[2]])   #取第二个位置的值
print(ts['2017-05'])    #取5月份的值
print(ts[datetime(2017,5,20):])#取5月20日之后的值
date2 = pd.date_range(start='5/1/2017',end='6/1/2017',freq='W-MON') #从5月1日到6月1日,每周的周一
print(date2)
image.png

日期的环比和后移

date2 = pd.date_range(start='5/1/2017',end='5/31/2017',freq='W-MON') #从5月1日到6月1日,每周的周一
print(date2)
ts1=ts[date2]# ts1 为5月每周一的值
print(ts1/ts1.shift(1)-1)  #周一环比增长率

from pandas.tseries.offsets import Day,MonthEnd
now=datetime(2011,11,17)
print(now+3*Day()) # 取三天后的日期

print(now+MonthEnd(1)) #取一个月后的最后一天

Period

Period表示一段时间,比如几天,几个月,几年


image.png
ss=pd.period_range('9/1/2007','6/9/2017',freq='M') #月维度,从07年9月到17年6月
print(ss)
p = pd.Period('2007',freq='A-JUN') #以6月为终点,7月为1年的开始
print(p.asfreq('M','start'))
print(p.asfreq('M','end'))
p1 = pd.Period('2012Q4',freq='Q-JAN') #以1月为一个季度的终点,那么11月1日为开始,1月31日为结束
print(p1.asfreq('D','start'))
print(p1.asfreq('D','end'))

Timestamp<=>Period

rng=pd.date_range('1/1/2000',periods=3,freq='M')
ts =Series(np.random.randn(3),index=rng)
print(ts)
pts=ts.to_period() #将日期转换成以月为单位的period
print(pts)
print(pts.to_timestamp(how='end')) #将月转换成月末最后一天
index = pd.PeriodIndex(year=data.year,quarter=data.quarter,freq='Q-DEC') #creating a periodindex from arrays

相关文章

  • python中unix时间戳、字符串、datetime之间的转换

    1. 将python的datetime转换为unix时间戳 2. 将unix时间戳转换为python的dateti...

  • Python 时间转换

    字符串转datetime datetime 转字符串 时间段

  • python 时间转换

    时间格式有字符串、时间数组、时间戳,用到的主要datetime和time 1.字符串转换成datetime、时间数...

  • python 时间转换

    1.取当前时间的日期格式: time.strftime('%Y-%m-%d %H:%M:%S',time.loca...

  • python 时间转换

    # 日期时间字符串st = "2017-11-23 16:10:10"# 当前日期时间dt = datetime....

  • Python的时间转换

    Python 的 Time Series 函数在日常数据分析中是最常应用的。任何一笔订单交易的情况,都可以转化成在...

  • Python的时间转换

    一、获取当前时间: 二、时间比较: 包含时分秒 不包含时分秒 三、各种转换demo: 时间戳转datetime类型...

  • [Python3]时间戳与时间的相互转换

    [Python3]时间戳与时间的相互转换 《转发》 摘要 在用Python处理数据时,可能有时候会需要将时间转换成...

  • learning

    python在终端进行文件的运行在终端输入 python test.py 时间时间的转换 编码python编码问题

  • python 时间戳转换

    startTime='2017-07-07 12:00:00'starttimeArray= time.strpt...

网友评论

      本文标题:Python的时间转换

      本文链接:https://www.haomeiwen.com/subject/ifjofxtx.html