Pandas Timestamp 常用属性详解
本文介绍 pandas.Timestamp 类中几个常用的时间相关属性,包括季度、年份判断及微秒获取等。
is_quarter_start
语法
pandas.Timestamp.is_quarter_start
参数
无
功能
用于判断一个特定的 Timestamp 对象是否是该季度的第一天。
返回值
当日期是该季度的第一天时,返回 True;否则,返回 False。
代码示例
import pandas as pd
# 创建一些 Timestamp 对象
timestamp1 = pd.Timestamp('2024-01-01') # 第一季度的第一天
timestamp2 = pd.Timestamp('2024-01-15') # 不是季度的第一天
timestamp3 = pd.Timestamp('2024-04-01') # 第二季度的第一天
# 判断是否是季度开始
is_quarter_start1 = timestamp1.is_quarter_start
is_quarter_start2 = timestamp2.is_quarter_start
is_quarter_start3 = timestamp3.is_quarter_start
print(is_quarter_start1)
print(is_quarter_start2)
print(is_quarter_start3)
结果输出
True
False
True
is_year_end
语法
pandas.Timestamp.is_year_end
参数
无
功能
用于判断一个特定的 Timestamp 对象是否是该年的最后一天。
返回值
当日期是该年的最后一天 (即 12 月 31 日) 时,返回 True;否则,返回 False。
代码示例
import pandas as pd
# 创建一些 Timestamp 对象
timestamp1 = pd.Timestamp('2024-12-31') # 年的最后一天
timestamp2 = pd.Timestamp('2024-11-30')
timestamp3 = pd.Timestamp()
is_year_end1 = timestamp1.is_year_end
is_year_end2 = timestamp2.is_year_end
is_year_end3 = timestamp3.is_year_end
(is_year_end1)
(is_year_end2)
(is_year_end3)


