Python酷库之旅-第三方库Pandas(140)

目录

一、用法精讲

631、pandas.Timestamp类

631-1、语法

631-2、参数

631-3、功能

631-4、返回值

631-5、说明

631-6、用法

631-6-1、数据准备

631-6-2、代码示例

631-6-3、结果输出

632、pandas.Timestamp.asm8属性

632-1、语法

632-2、参数

632-3、功能

632-4、返回值

632-5、说明

632-6、用法

632-6-1、数据准备

632-6-2、代码示例

632-6-3、结果输出

633、pandas.Timestamp.day属性

633-1、语法

633-2、参数

633-3、功能

633-4、返回值

633-5、说明

633-6、用法

633-6-1、数据准备

633-6-2、代码示例

633-6-3、结果输出

634、pandas.Timestamp.dayofweek属性

634-1、语法

634-2、参数

634-3、功能

634-4、返回值

634-5、说明

634-6、用法

634-6-1、数据准备

634-6-2、代码示例

634-6-3、结果输出

635、pandas.Timestamp.day_of_week属性

635-1、语法

635-2、参数

635-3、功能

635-4、返回值

635-5、说明

635-6、用法

635-6-1、数据准备

635-6-2、代码示例

635-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

631、pandas.Timestamp类
631-1、语法
python 复制代码
# 631、pandas.Timestamp类
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_inputdatetime-like, str, int, float
Value to be converted to Timestamp.

year, month, dayint
hour, minute, second, microsecondint, optional, default 0
tzinfodatetime.tzinfo, optional, default None
nanosecondint, optional, default 0
tzstr, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.

unitstr
Unit used for conversion if ts_input is of type int or float. The valid values are 'D', 'h', 'm', 's', 'ms', 'us', and 'ns'. For example, 's' means seconds and 'ms' means milliseconds.

For float inputs, the result will be stored in nanoseconds, and the unit attribute will be set as '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.

Notes

There are essentially three calling conventions for the constructor. The primary form accepts four parameters. They can be passed by position or keyword.

The other two forms mimic the parameters from datetime.datetime. They can be passed by either position or keyword, but not both mixed together.
631-2、参数

631-2-1、ts_input**(可选,默认值为<object object>)****:**这可以是一个字符串、数值、datetime对象或者其他可以被解释为时间戳的对象,如果提供了ts_input,其他单独的时间参数(如year, month等)会被忽略。

631-2-2、year**(可选,默认值为None)****:**指定年份(非负整数)。

631-2-3、month**(可选,默认值为None)****:**指定月份(1到12的整数)。

631-2-4、day**(可选,默认值为None)****:**指定日期(1到31的整数,具体取决于月份和年份)。

631-2-5、hour**(可选,默认值为None)****:**指定小时(0到23的整数)。

631-2-6、minute**(可选,默认值为None)****:**指定分钟(0到59的整数)。

631-2-7、second**(可选,默认值为None)****:**指定秒数(0到59的整数)。

631-2-8、microsecond**(可选,默认值为None)****:**指定微秒数(0到999999的整数)。

631-2-9、tzinfo**(可选,默认值为None)****:**一个tzinfo对象,表示时区信息。

631-2-10、nanosecond**(可选,默认值为None)****:**指定纳秒(0到999999999的整数)。

631-2-11、tz**(可选,默认值为None)****:**时区名称或tzinfo对象,可以用于设置时间戳的时区,此参数为可选参数,如果提供则默认为None。

631-2-12、unit**(可选,默认值为None)****:**字符串,指定ts_input值的单位。例如's'(秒)、'ms'(毫秒)、'us'(微秒)、'ns'(纳秒)等等。

631-2-13、fold**(可选,默认值为None)****:**仅仅用于支持Python 3.6及以上的PEP 495,表示时间折叠,在有DST变化时,时间会重复出现两次。

631-3、功能

用于创建和操作时间戳,它提供了多种方法来格式化、比较和解析时间数据,它兼容datetime.datetime,因此可以与标准Python的日期和时间处理方法互操作

631-4、返回值

返回一个时间戳对象,该对象封装了日期和时间信息,并提供了一些有用的方法和属性来操作时间数据。

631-5、说明

无

631-6、用法
631-6-1、数据准备
python 复制代码
无
631-6-2、代码示例
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)
631-6-3、结果输出
python 复制代码
# 631、pandas.Timestamp类
# 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
632、pandas.Timestamp.asm8属性
632-1、语法
python 复制代码
# 632、pandas.Timestamp.asm8属性
pandas.Timestamp.asm8
Return numpy datetime64 format in nanoseconds.
632-2、参数

无

632-3、功能

将一个pandas.Timestamp对象转换为NumPy的datetime64对象,这对于需要进行高效矢量化操作或与NumPy数组进行兼容性处理的操作特别有用。

632-4、返回值

返回一个NumPy datetime64对象,其值与Timestamp对象表示的日期和时间相对应。

632-5、说明

无

632-6、用法
632-6-1、数据准备
python 复制代码
无
632-6-2、代码示例
python 复制代码
# 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)
632-6-3、结果输出
python 复制代码
# 632、pandas.Timestamp.asm8属性
# <class 'numpy.datetime64'> 2024-10-04T18:47:00
633、pandas.Timestamp.day属性
633-1、语法
python 复制代码
# 633、pandas.Timestamp.day属性
pandas.Timestamp.day.
633-2、参数

无

633-3、功能

从一个pandas.Timestamp对象中提取并返回其表示的日期中的"日",这对于需要从时间戳中获取具体日期信息的场景,尤其是需要对日期进行过滤、分组或显示格式化等操作时非常有用。

633-4、返回值

返回一个整数值,表示该Timestamp对象对应日期是一个月中的第几天。

633-5、说明

无

633-6、用法
633-6-1、数据准备
python 复制代码
无
633-6-2、代码示例
python 复制代码
# 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)
633-6-3、结果输出
python 复制代码
# 633、pandas.Timestamp.day属性
# 4
# 15
634、pandas.Timestamp.dayofweek属性
634-1、语法
python 复制代码
# 634、pandas.Timestamp.dayofweek属性
pandas.Timestamp.dayofweek
Return day of the week.

Returns:
int
634-2、参数

无

634-3、功能

从一个pandas.Timestamp对象中提取并返回其表示的星期几,对于需要对日期进行特定的逻辑条件检查(例如,判断是否是工作日或周末)以及分组分析的场景,这一特性非常有帮助。

634-4、返回值

返回一个整数值,表示该Timestamp对象对应日期的星期几,其中0代表星期一,1代表星期二,以此类推,直到6代表星期日。

634-5、说明

无

634-6、用法
634-6-1、数据准备
python 复制代码
无
634-6-2、代码示例
python 复制代码
# 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)
634-6-3、结果输出
python 复制代码
# 634、pandas.Timestamp.dayofweek属性
# 4
# 1
635、pandas.Timestamp.day_of_week属性
635-1、语法
python 复制代码
# 635、pandas.Timestamp.day_of_week属性
pandas.Timestamp.day_of_week
Return day of the week.

Returns:
int
635-2、参数

无

635-3、功能

从一个pandas.Timestamp对象中提取并返回其表示的星期几,这对于需要日期分析和逻辑条件判断的场景非常有用。例如,判断某天是否是工作日或周末,或者在分组分析中按星期几分组等。

635-4、返回值

返回一个整数值,表示该Timestamp对象对应日期的星期几,0代表星期一,1代表星期二,一直到6代表星期日。

635-5、说明

无

635-6、用法
635-6-1、数据准备
python 复制代码
无
635-6-2、代码示例
python 复制代码
# 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)
635-6-3、结果输出
python 复制代码
# 635、pandas.Timestamp.day_of_week属性
# 6
# 5

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
相关推荐
GIS数据转换器2 小时前
地下空间综合管廊运营管理平台
大数据·开发语言·人工智能·经验分享·perl
Wang's Blog7 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图
java·开发语言·状态模式·交友
dozenyaoyida7 小时前
AI与大模型新闻日报 | 2026-10-17
人工智能·ai·chatgpt·新闻
遇码7 小时前
流式调用中途断连别直接报错:瞬态故障识别、退避重试与中文根因诊断的搭建方法
大数据·人工智能·ai·rust·数据分析·大模型·agent
信誓旦旦的程序猿7 小时前
【Python量化实战宝典 #08】可转债与债券接口手册:转股价·溢价率·纯债价值(Python 可跑)
java·python·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
救救孩子把8 小时前
AI面试八股文-21_21-多模态Agent面试题-Vision-Language-Agent
人工智能·面试·职场和发展·llava·多模态agent
砚底藏山河8 小时前
python量化:行情数据回溯
java·开发语言·python·金融·maven
今年下半年8 小时前
AI智能体项目实战记录(02):初始化 Python 项目
开发语言·人工智能·python·fastapi
reasonsummer8 小时前
【办公类-53-12】20261016(预测)Python模仿制作2026学年第二学期校历(excel+pdf,上下学期都包含)
pdf·excel