import pandas as pd
import datetime
import calendar
1 创建时间戳
pd.Timestamp('19990227')
Timestamp('1999-02-27 00:00:00')
pd.Timestamp('1999-02-27')
Timestamp('1999-02-27 00:00:00')
pd.Timestamp('1999-2-27')
Timestamp('1999-02-27 00:00:00')
pd.Timestamp(1999, 2, 27)
Timestamp('1999-02-27 00:00:00')
2 时间加减
time = pd.Timestamp(1999, 2, 27)
delta = pd.Timedelta(days = 1)
time = time + delta
time
Timestamp('1999-02-28 00:00:00')
3 判断变量是否为时间戳类型
isinstance(time, datetime.datetime)
True
4 获取当月天数
calendar.monthrange(1999, 2)[1]
28
5 参考资料
pandas处理时间序列(1)
pandas中Timestamp类用法讲解
【python】如何判断一个变量是日期类型,还是日期形式的字符串
python怎么样获取当月有多少天?
本文最新版本