![5f7f3b3f10355af09200399ef298225b.png](https://i-blog.csdnimg.cn/blog_migrate/b19f33af0ac8713cc124a6d4117f20a0.jpeg)
时间模块: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()