Python学习之日期与时间

时间与日期类型的定义

定义一个标准格式时间类型的对象的方法:datatime包

  1. 定义时间类型的数据
import datetime as dt

myTime = dt.time(12, 34, 59)
print(myTime)  # 时间
print(myTime.hour)  # 小时
print(myTime.minute)  # 分钟
print(myTime.second)  # 秒
  1. 定义日期类型的数据
import datetime as dt

myTime = dt.datetime(year=2020, month=2, day=19)  # 在datetime中year、month、day必选,其他值不写默认为0
print(myTime)
# 2020-02-19 00:00:00
print(dt.datetime(year=2020, month=2, day=19, hour=12, minute=34, second=59))
# 2020-02-19 12:34:59

格式转换

只有标准格式时间数据才能作为dt.datetime()的参数,如2020-02-19 12:34:59
非标准时间数据进行格式转换

  1. 用dateutil包中的parser()
 from dateutil import parser

print(parser.parse("3th of July,2018"))
#2018-07-03 00:00:00
print(parser.parse("2019-1-3"))
#2019-01-03 00:00:00
  1. 用pandas包中的to_datetime()
import pandas as pd

# to_datetime返回类型为Timestamp
print(pd.to_datetime("3th of July,2018"))
# 2018-07-03 00:00:00
print(pd.to_datetime("2019-1-3"))
# 2019-01-03 00:00:00

显示系统当前时间

import datetime as dt

print(dt.datetime.now())
#2020-02-19 14:29:30.471301
print(dt.datetime.today())
#2020-02-19 14:29:57.629225
now = dt.datetime.now()
print(now.strftime("%W"))#07 %W一年中的星期数(0~53)
print(now.strftime("%a"))#Wed %a星期几的简写
print(now.strftime("%A"))#Wednesday %A星期几的全称
print(now.strftime("%b"))#Feb %b月份的简写
print(now.strftime("%B"))#February %B月份的全称
print(now.strftime("%c"))#Wed Feb 19 14:35:41 2020 %c标准的日期的时间串
print(now.strftime("%C"))#20 %C年份的后两位数字
print(now.strftime("%d"))#19 %d十进制表示的每月的第几天
print(now.strftime("%D"))#02/19/20 %D月/天/年

关于strftime()的更多用法可以看

https://blog.csdn.net/SanQi_Abao/article/details/94964665

计算时间差

import datetime as dt

d1 = dt.datetime.now()
d2 = dt.datetime(year=2017,month=3,day=3)
print((d1-d2))
#1083 days, 14:40:29.053038
print((d1-d2).days)
#1083

时间索引

用pandas包中的DatetimeIndex()可以作为索引

import pandas as pd

Index = pd.DatetimeIndex(["2018-1-1", "2019-1-2", "2018-1-3", "2018-1-4", "2018-1-5"])
Date = pd.Series(data=[1, 2, 3, 4, 5], index=Index)
print(Date)
'''
2018-01-01    1
2019-01-02    2
2018-01-03    3
2018-01-04    4
2018-01-05    5
dtype: int64
Series([], dtype: int64)
'''
print(Date["2018-1-2"])
#Series([], dtype: int64)
print(Date["2018"])#可将2019的过滤掉
'''
2018-01-01    1
2018-01-03    3
2018-01-04    4
2018-01-05    5
dtype: int64
'''
print(Date-Date["2018-1-4"])#支持减法操作
'''
2018-01-01    NaN
2018-01-03    NaN
2018-01-04    0.0
2018-01-05    NaN
2019-01-02    NaN
dtype: float64
'''
print(Date-Date["20180104"])
'''
2018-01-01    NaN
2018-01-03    NaN
2018-01-04    0.0
2018-01-05    NaN
2019-01-02    NaN
dtype: float64
'''
print(Date-Date[3])#3为隐式索引,对应的是2018-01-04 4
'''
2018-01-01   -3
2019-01-02   -2
2018-01-03   -1
2018-01-04    0
2018-01-05    1
dtype: int64
'''
print(Date.to_period(freq='D'))
'''
2018-01-01    1
2019-01-02    2
2018-01-03    3
2018-01-04    4
2018-01-05    5
Freq: D, dtype: int64
'''
print(Date.to_period(freq='M'))
'''
2018-01    1
2019-01    2
2018-01    3
2018-01    4
2018-01    5
Freq: M, dtype: int64
'''

period_range()函数

类似于range()和numpy中的arange()
period_range()
第一个参数为起始时间
periods = ,表示时间单位个数
freq = ,代表时间单位,‘Y’、‘M’、‘D’分布年、月、日

import pandas as pd

print(pd.period_range("2020-1", periods=5, freq='D'))
'''
PeriodIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
             '2020-01-05'],
            dtype='period[D]', freq='D')
'''
print(pd.period_range("2020-1", periods=5, freq='M'))
# PeriodIndex(['2020-01', '2020-02', '2020-03', '2020-04', '2020-05'], dtype='period[M]', freq='M')
print(pd.period_range("2020-1", periods=5, freq='Y'))
# PeriodIndex(['2020', '2021', '2022', '2023', '2024'], dtype='period[A-DEC]', freq='A-DEC')
发布了28 篇原创文章 · 获赞 0 · 访问量 855

猜你喜欢

转载自blog.csdn.net/weixin_43866408/article/details/104392353