pandas提取时间里面的年月日_python入门 | pandas的datetime

5f7f3b3f10355af09200399ef298225b.png

时间模块:datetime

1.datetime.date:date对象 年月日

  • datetime.date.today()
  • 该对象类型为datetime.date
  • 可以通过str函数转化为str
In [1]: import datetime

In [2]: today = datetime.date.today()

In [3]: today
Out[3]: datetime.date(2020, 4, 28)

In [4]: print(today,type(today))
2020-04-28 <class 'datetime.date'>

In [5]: print(str(today))
2020-04-28

2.datetime.datetime:datetime对象 年月日时分秒-

  • datetime.datetime.now() datetime.datetime.today()
  • 对象类型为datetime.datetime
  • 可以通过str函数转为str
In [6]: now = datetime.datetime.now()

In [7]: now
Out[7]: datetime.datetime(2020, 4, 28, 22, 48, 16, 780405)

In [8]: print(now,type(now))
2020-04-28 22:48:16.780405 <class 'datetime.datetime'>

In [9]: print(str(now),type(str(now)))
2020-04-28 22:48:16.780405 <class 'str'>
  • 创建datetime对象:datetime.datetime(x,x,x,x,x)
  • 无时间默认0点
In [10]: t1 = datetime.datetime(2016,6,1)

In [11]: t2 = datetime.datetime(2014,1,1,12,44,33)

In [12]: print(t1,t2)
2016-06-01 00:00:00 2014-01-01 12:44:33
  • 相减得到时间差
In [13]: t2-t1
Out[13]: datetime.timedelta(days=-882, seconds=45873)

3.datetime.timedelta:时间差

  • datetime.timedelta(number)
In [14]: yesterday = today - datetime.timedelta(1)

In [15]: print(today,yesterday)
2020-04-28 2020-04-27

(pandas的datetime和period具体里都可以精确到秒,只是总体上看单个datetime输出是单日,单个period输出是时期)

Pandas的单日:Datetime

(Pandas时刻数据:Timestamp) 与 (Pandas 时间戳索引:DatetimeIndex)

输入的日期各种格式都可以解析,但无法支持中文

'2017-12-21'   '20170101'   '1/13/2017'   '2017/1/1'

1 介绍

1.创建单个timestamp:

  • pd.Timestamp(时间数据) 年月日时分秒
In [20]: t1 = pd.Timestamp( datetime.datetime(2016,12,1,12,45,30) )

In [21]: t2 = pd.Timestamp('2017-12-21')

In [24]: t3 = pd.Timestamp('2017-12-21 15:00:22')

In [25]: print(t3)
2017-12-21 15:00:22

In [22]: print(t1,type(t1))
2016-12-01 12:45:30 <class 'pandas._libs.tslibs.timestamps.Timestamp'>

In [23]: print(t2,type(t2))
2017-12-21 00:00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
  • pd.to_datetime(时间数据)
In [26]: t1 = pd.to_datetime(datetime.datetime(2016,12,1,12,45))

In [27]: t2 = pd.to_datetime('2017-12-21 15:00:22')

In [28]: print(t1,t2)
2016-12-01 12:45:00 2017-12-21 15:00:22

2. 创建时间戳索引:

  • pd.to_datetime(时间数据)
  • 序列中夹着其他数据时:一定要参数errors = ‘ignore’(直接生成一般数组)/‘coerce’(缺失值生成NaT,返回datetimeindex
In [29]: st_date = [ '2017-12-21', '2017-12-22', '2017-12-23']

In [30]: t3 = pd.to_datetime(st_date)

In [31]: print(t3,type(t3))
DatetimeIndex(['2017-12-21', '2017-12-22', '2017-12-23'], dtype='datetime64[ns]', freq=None) <class 'pandas.core.indexes.datetimes.DatetimeIndex'>


# 参数errors
In [35]: date3 = ['2017-2-1','2017-2-2','2017-2-3','hello world!','2017-2-5','2017-2-6']

In [36]: t3 = pd.to_datetime(date3, errors = 'ignore')

In [37]: t4 = pd.to_datetime(date3, errors = 'coerce')

In [38]: t5 = pd.to_datetime(date3) # 报错

In [39]: print(t3, type(t3))
Index(['2017-2-1', '2017-2-2', '2017-2-3', 'hello world!', '2017-2-5',
       '2017-2-6'],
      dtype='object') <class 'pandas.core.indexes.base.Index'>

In [40]: print(t4, type(t4))
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', 'NaT', '2017-02-05',
               '2017-02-06'],
              dtype='datetime64[ns]', freq=None) <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
  • pd.DatetimeIndex()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值