一、datetime模块
1、datetime模块是Python中处理日期和时间的标准库之一。它主要包含4个类:datetime、date、time和timedelta。
2、datetime.datetime类是用来表示日期和时间的类。它包含了年、月、日、时、分、秒和毫秒等各个部分。通常我们使用datetime.now()方法来获取当前时间。
import datetime
now = datetime.datetime.now()
print(now)
# 输出:2022-03-07 10:22:33.123456
3、datetime.date类是用来表示日期的类,它只包含年、月、日。通常我们使用date.today()方法来获取当前日期。
import datetime
today = datetime.date.today()
print(today)
# 输出:2022-03-07
4、datetime.time类是用来表示时间的类,它只包含时、分、秒和毫秒等各个部分。
import datetime
t = datetime.time(hour=10, minute=22, second=33, microsecond=123456)
print(t)
# 输出:10:22:33.123456
5、datetime.timedelta类是用来表示时间差的类,可以非常方便地进行日期和时间的加减操作。
import datetime
today = datetime.date.today()
delta = datetime.timedelta(days=30)
thirty_days_later = today + delta
print(thirty_days_later)
# 输出:2022-04-06
二、time模块
1、time模块是Python中处理时间的标准库之一。它主要包含了sleep()、asctime()、localtime()、strptime()等方法。
2、time.sleep()方法可以让程序暂停一段时间,通常用于模拟耗时操作。
import time
print('start')
time.sleep(3)
print('end')
3、time.asctime()方法可以将时间元组转换成字符串。
import time
t = (2022, 3, 7, 10, 22, 33, 0, 0, 0)
str_time = time.asctime(t)
print(str_time)
# 输出:Mon Mar 7 10:22:33 2022
4、time.localtime()方法可以将时间戳转换成本地时间。
import time
t = time.time()
local_time = time.localtime(t)
print(local_time)
# 输出:time.struct_time(tm_year=2022, tm_mon=3, tm_mday=7, tm_hour=10, tm_min=22, tm_sec=33, tm_wday=0, tm_yday=66, tm_isdst=0)
5、time.strptime()方法可以将字符串转换成时间元组。
import time
str_time = '2022-03-07 10:22:33'
time_tuple = time.strptime(str_time, '%Y-%m-%d %H:%M:%S')
print(time_tuple)
# 输出:time.struct_time(tm_year=2022, tm_mon=3, tm_mday=7, tm_hour=10, tm_min=22, tm_sec=33, tm_wday=0, tm_yday=66, tm_isdst=-1)
三、calendar模块
1、calendar模块是Python中处理日历的标准库之一。它主要包含calendar()、month()、prmonth()等方法。
2、calendar.calendar()方法可以输出一整年的日历。
import calendar
calendar_str = calendar.calendar(2022)
print(calendar_str)
# 输出:2022年的日历
3、calendar.month()方法可以输出一个月的日历。
import calendar
calendar_str = calendar.month(2022, 3)
print(calendar_str)
# 输出:2022年3月的日历
4、calendar.prmonth()方法和month()方法类似,只是输出的结果会直接打印到控制台中。
import calendar
calendar.prmonth(2022, 3)
# 直接在控制台中输出2022年3月的日历
四、arrow模块
1、arrow模块是Python中比较新的时间处理库,它包含了很多方便的方法,比如humanize()、datetime()、parse()等。
2、arrow.get()方法可以将一个字符串转换成arrow对象。
import arrow
a = arrow.get('2022-03-07 10:22:33')
print(a)
# 输出:2022-03-07T10:22:33+00:00
3、arrow.datetime()方法可以将arrow对象转换成datetime对象。
import arrow
a = arrow.get('2022-03-07 10:22:33')
d = a.datetime
print(d)
# 输出:2022-03-07 10:22:33+00:00
4、arrow.humanize()方法可以将日期转换成易于阅读的格式。
import arrow
a = arrow.now()
humanize_time = a.humanize()
print(humanize_time)
# 输出:moments ago
五、pandas模块
1、pandas模块是Python中处理数据的常用库之一。它可以非常方便地处理时间序列数据。
2、pandas.to_datetime()方法可以将字符串转换成pandas的时间类型。
import pandas as pd
df = pd.DataFrame({'date':['2022-03-07', '2022-03-08', '2022-03-09'], 'value':[1, 2, 3]})
df['date'] = pd.to_datetime(df['date'])
print(df)
# 输出:包含日期和值的DataFrame
3、pandas.date_range()方法可以生成一段时间序列。
import pandas as pd
start_date = '2022-03-01'
end_date = '2022-03-31'
date_range = pd.date_range(start=start_date, end=end_date)
print(date_range)
# 输出:包含整个三月份日期的时间序列
4、pandas.resample()方法可以对时间序列进行重新采样。
import pandas as pd
df = pd.DataFrame({'date':['2022-03-07', '2022-03-08', '2022-03-09'], 'value':[1, 2, 3]})
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
df_resampled = df.resample('D').sum()
print(df_resampled)
# 输出:包含3月份每一天总和的时间序列
六、总结
1、Python提供了多个处理时间的标准库和第三方库,每个库都有其独特的优势和应用场景。
2、在实际应用中,根据具体的需求选择合适的时间库能够大幅减少代码的编写难度和维护成本。
3、数据分析师需要掌握至少一两个时间处理库,并且能够熟练地利用其中的方法完成时间的加减、转换和格式化等工作。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/244988.html
微信扫一扫
支付宝扫一扫