Python 第三方库 Pandas Timestamp 类及属性用法精讲
pandas 库中 Timestamp 类的创建方法及常用属性。内容包括通过字符串、参数、时区或数值创建时间戳,以及 asm8 属性转换 numpy 对象。此外还讲解了 day 属性获取日期中的日,dayofweek 和 day_of_week 属性获取星期几的用法与返回值说明,并提供了相应的代码示例和结果输出。

pandas 库中 Timestamp 类的创建方法及常用属性。内容包括通过字符串、参数、时区或数值创建时间戳,以及 asm8 属性转换 numpy 对象。此外还讲解了 day 属性获取日期中的日,dayofweek 和 day_of_week 属性获取星期几的用法与返回值说明,并提供了相应的代码示例和结果输出。

class pandas.Timestamp(ts_input=<object object>, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, nanosecond=None, tz=None, unit=None, fold=None)
Pandas replacement for python datetime.datetime object. Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
Parameters:
ts_input: datetime-like, str, int, float. Value to be converted to Timestamp.year, month, day: int. Optional, default None.hour, minute, second, microsecond: int, optional, default 0.tzinfo: datetime.tzinfo, optional, default None.nanosecond: int, optional, default 0.tz: str, pytz.timezone, dateutil.tz.tzfile or None. Time zone for time which Timestamp will have.unit: str. Unit used for conversion if ts_input is of type int or float. Valid values are 'D', 'h', 'm', 's', 'ms', 'us', and 'ns'.fold: {0, 1}, default None, keyword-only. Due to daylight saving time, one wall clock time can occur twice when shifting from summer to winter time; fold describes whether the datetime-like corresponds to the first (0) or the second time (1) the wall clock hits the ambiguous time.用于创建和操作时间戳,它提供了多种方法来格式化、比较和解析时间数据,它兼容 datetime.datetime,因此可以与标准 Python 的日期和时间处理方法互操作。
返回一个时间戳对象,该对象封装了日期和时间信息,并提供了一些有用的方法和属性来操作时间数据。
无
无
# 631、pandas.Timestamp 类
import pandas as pd
# 使用字符串创建 Timestamp
timestamp1 = pd.Timestamp('2024-10-04 18:47:00')
print(timestamp1)
# 使用单独的参数创建 Timestamp
timestamp2 = pd.Timestamp(year=2024, month=10, day=4, hour=18, minute=47, second=0)
print(timestamp2)
# 使用时区创建 Timestamp
timestamp3 = pd.Timestamp('2024-10-04 18:47:00', tz='UTC')
print(timestamp3)
# 使用数值和单位创建 Timestamp
timestamp4 = pd.Timestamp(1728067620.0, unit='s')
print(timestamp4)
# 查看 Timestamp 对象的属性
print("Year:", timestamp1.year)
print("Month:", timestamp1.month)
print("Day:", timestamp1.day)
print("Hour:", timestamp1.hour)
print("Minute:", timestamp1.minute)
print("Second:", timestamp1.second)
print("Microsecond:", timestamp1.microsecond)
print("Nanosecond:", timestamp1.nanosecond)
print("Time zone:", timestamp3.tz)
# 2024-10-04 18:47:00
# 2024-10-04 18:47:00
# 2024-10-04 18:47:00+00:00
# 2024-10-04 18:47:00
# Year: 2024
# Month: 10
# Day: 4
# Hour: 18
# Minute: 47
# Second: 0
# Microsecond: 0
# Nanosecond: 0
# Time zone: UTC
pandas.Timestamp.asm8
Return numpy datetime64 format in nanoseconds.
无
将一个 pandas.Timestamp 对象转换为 NumPy 的 datetime64 对象,这对于需要进行高效矢量化操作或与 NumPy 数组进行兼容性处理的操作特别有用。
返回一个 NumPy datetime64 对象,其值与 Timestamp 对象表示的日期和时间相对应。
无
无
# 632、pandas.Timestamp.asm8 属性
import pandas as pd
# 创建一个 Timestamp 对象
timestamp = pd.Timestamp('2024-10-04 18:47:00')
# 使用 asm8 属性获取 NumPy datetime64 对象
numpy_datetime = timestamp.asm8
print(type(numpy_datetime), numpy_datetime)
<class 'numpy.datetime64'> 2024-10-04T18:47:00
pandas.Timestamp.day
无
从一个 pandas.Timestamp 对象中提取并返回其表示的日期中的'日',这对于需要从时间戳中获取具体日期信息的场景,尤其是需要对日期进行过滤、分组或显示格式化等操作时非常有用。
返回一个整数值,表示该 Timestamp 对象对应日期是一个月中的第几天。
无
无
# 633、pandas.Timestamp.day 属性
import pandas as pd
# 获取日期中的"日"部分
timestamp1 = pd.Timestamp('2024-10-04 18:47:00')
print(timestamp1.day)
# 应用在 Timestamp 的另一种创建方式
timestamp2 = pd.Timestamp(year=2024, month=8, day=15, hour=12, minute=30)
print(timestamp2.day)
# 4
# 15
pandas.Timestamp.dayofweek
Return day of the week. Returns: int
无
从一个 pandas.Timestamp 对象中提取并返回其表示的星期几,对于需要对日期进行特定的逻辑条件检查 (例如,判断是否是工作日或周末) 以及分组分析的场景,这一特性非常有帮助。
返回一个整数值,表示该 Timestamp 对象对应日期的星期几,其中 0 代表星期一,1 代表星期二,以此类推,直到 6 代表星期日。
无
无
# 634、pandas.Timestamp.dayofweek 属性
import pandas as pd
# 获取日期对应的星期几
timestamp1 = pd.Timestamp('2024-10-04 18:47:00')
print(timestamp1.dayofweek)
# 应用于不同的日期
timestamp2 = pd.Timestamp('2024-10-15 12:30:00')
print(timestamp2.dayofweek)
# 4
# 1
pandas.Timestamp.day_of_week
Return day of the week. Returns: int
无
从一个 pandas.Timestamp 对象中提取并返回其表示的星期几,这对于需要日期分析和逻辑条件判断的场景非常有用。例如,判断某天是否是工作日或周末,或者在分组分析中按星期几分组等。
返回一个整数值,表示该 Timestamp 对象对应日期的星期几,0 代表星期一,1 代表星期二,一直到 6 代表星期日。
无
无
# 635、pandas.Timestamp.day_of_week 属性
import pandas as pd
# 获取日期对应的星期几
timestamp1 = pd.Timestamp('2024-10-06 18:47:00')
print(timestamp1.day_of_week)
# 应用于不同日期
timestamp2 = pd.Timestamp('2024-06-15 12:30:00')
print(timestamp2.day_of_week)
# 6
# 5

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,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
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online