Pandas Timestamp 常用属性详解
本文介绍 pandas.Timestamp 类中几个常用的时间相关属性,包括季度、年份判断及微秒获取等。
pandas.Timestamp 类的五个常用属性:is_quarter_start 判断是否为季度首日,is_year_end 判断是否为年末,is_year_start 判断是否为年初,max 获取最大时间戳,microsecond 获取微秒值。通过代码示例展示了各属性的具体用法和返回值,帮助开发者高效处理时间数据。

本文介绍 pandas.Timestamp 类中几个常用的时间相关属性,包括季度、年份判断及微秒获取等。
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
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('2024-01-01') # 新年的第一天
# 判断是否是年末
is_year_end1 = timestamp1.is_year_end
is_year_end2 = timestamp2.is_year_end
is_year_end3 = timestamp3.is_year_end
print(is_year_end1)
print(is_year_end2)
print(is_year_end3)
True
False
False
pandas.Timestamp.is_year_start
无
用于检查一个特定的 Timestamp 对象是否为该年的第一天。
如果该日期是 1 月 1 日 (即该年的第一天),则返回 True;否则,返回 False。
import pandas as pd
# 创建一些 Timestamp 对象
timestamp1 = pd.Timestamp('2024-01-01') # 年的第一天
timestamp2 = pd.Timestamp('2024-06-15') # 不是年的第一天
timestamp3 = pd.Timestamp('2024-01-01') # 新年的第一天
# 判断是否是年初
is_year_start1 = timestamp1.is_year_start
is_year_start2 = timestamp2.is_year_start
is_year_start3 = timestamp3.is_year_start
print(is_year_start1)
print(is_year_start2)
print(is_year_start3)
True
False
True
pandas.Timestamp.max = Timestamp('2262-04-11 23:47:16.854775807')
无
表示 Timestamp 类型可以表示的最大日期和时间。
返回一个 Timestamp 对象,代表能够表示的最大日期,通常为 2262-04-11 23:47:16.854775807。
import pandas as pd
# 获取 Timestamp 的最大值
max_timestamp = pd.Timestamp.max
print(max_timestamp)
2262-04-11 23:47:16.854775807
pandas.Timestamp.microsecond
无
用于获取时间戳中的微秒部分 (即 1/1,000,000 秒)。
返回一个整数,范围在 0 到 999,999 之间。
import pandas as pd
# 创建一个 Timestamp 对象
timestamp = pd.Timestamp('2024-10-10 12:34:56.789123')
# 获取微秒部分
microsecond_value = timestamp.microsecond
print(microsecond_value)
789123

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online