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

目录

一、用法精讲

671、pandas.Timestamp.day_name方法

671-1、语法

671-2、参数

671-3、功能

671-4、返回值

671-5、说明

671-6、用法

671-6-1、数据准备

671-6-2、代码示例

671-6-3、结果输出

672、pandas.Timestamp.dst方法

672-1、语法

672-2、参数

672-3、功能

672-4、返回值

672-5、说明

672-6、用法

672-6-1、数据准备

672-6-2、代码示例

672-6-3、结果输出

673、pandas.Timestamp.floor方法

673-1、语法

673-2、参数

673-3、功能

673-4、返回值

673-5、说明

673-6、用法

673-6-1、数据准备

673-6-2、代码示例

673-6-3、结果输出

674、pandas.Timestamp.fromordinal方法

674-1、语法

674-2、参数

674-3、功能

674-4、返回值

674-5、说明

674-6、用法

674-6-1、数据准备

674-6-2、代码示例

674-6-3、结果输出

675、pandas.Timestamp.fromtimestamp方法

675-1、语法

675-2、参数

675-3、功能

675-4、返回值

675-5、说明

675-6、用法

675-6-1、数据准备

675-6-2、代码示例

675-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

671、pandas.Timestamp.day_name方法
671-1、语法
python 复制代码
# 671、pandas.Timestamp.day_name方法
pandas.Timestamp.day_name(locale=None)
Return the day name of the Timestamp with specified locale.

Parameters:
locale
str, default None (English locale)
Locale determining the language in which to return the day name.

Returns:
str
671-2、参数

671-2-1、locale**(可选,默认值为None)****:**字符串,例如'en_US'、'fr_FR'、'de_DE'等,指定返回的星期几名称的语言环境,如果未指定,则使用当前系统的默认语言环境。

671-3、功能

根据传入的语言环境返回对应语言的星期几名称。

671-4、返回值

返回一个字符串,表示星期几的名称。例如,在英语中可能是'Monday'、'Tuesday'等;在法语中可能是'lundi'、'mardi'等。

671-5、说明

671-6、用法
671-6-1、数据准备
python 复制代码
671-6-2、代码示例
python 复制代码
# 671、pandas.Timestamp.day_name方法
import pandas as pd
# 创建一个时间戳
ts = pd.Timestamp('2024-10-14')
# 获取不同语言环境下的星期几名称
day_name_en = ts.day_name(locale='en_US')  # 默认是英语
day_name_fr = ts.day_name(locale='fr_FR')  # 法语
day_name_de = ts.day_name(locale='de_DE')  # 德语
print(day_name_en)
print(day_name_fr)
print(day_name_de) 
671-6-3、结果输出
python 复制代码
# 671、pandas.Timestamp.day_name方法
# Monday
# Lundi
# Montag
672、pandas.Timestamp.dst方法
672-1、语法
python 复制代码
# 672、pandas.Timestamp.dst方法
pandas.Timestamp.dst()
Return the daylight saving time (DST) adjustment.
672-2、参数

672-3、功能

检查时间戳是否处于夏令时期间,并返回相应的偏移量。

672-4、返回值

如果时间戳处于夏令时期间,返回一个timedelta对象,表示偏移量;如果时间戳不在夏令时期间,返回timedelta(0),表示没有偏移。

672-5、说明

672-6、用法
672-6-1、数据准备
python 复制代码
672-6-2、代码示例
python 复制代码
# 672、pandas.Timestamp.dst方法
import pandas as pd
# 创建一个时间戳
ts1 = pd.Timestamp('2024-06-15 12:00:00', tz='America/New_York')  # 夏令时期间
ts2 = pd.Timestamp('2024-11-15 12:00:00', tz='America/New_York')  # 非夏令时期间
# 获取夏令时偏移
dst_offset1 = ts1.dst()
dst_offset2 = ts2.dst()
print(dst_offset1)  # 输出:1:00:00 (表示1小时的偏移)
print(dst_offset2)  # 输出:0:00:00 (表示没有偏移)
672-6-3、结果输出
python 复制代码
# 672、pandas.Timestamp.dst方法
# 1:00:00
# 0:00:00
673、pandas.Timestamp.floor方法
673-1、语法
python 复制代码
# 673、pandas.Timestamp.floor方法
pandas.Timestamp.floor(freq, ambiguous='raise', nonexistent='raise')
Return a new Timestamp floored to this resolution.

Parameters:
freq
str
Frequency string indicating the flooring resolution.

ambiguous
bool or {'raise', 'NaT'}, default 'raise'
The behavior is as follows:

bool contains flags to determine if time is dst or not (note that this flag is only applicable for ambiguous fall dst dates).

'NaT' will return NaT for an ambiguous time.

'raise' will raise an AmbiguousTimeError for an ambiguous time.

nonexistent
{'raise', 'shift_forward', 'shift_backward, 'NaT', timedelta}, default 'raise'
A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST.

'shift_forward' will shift the nonexistent time forward to the closest existing time.

'shift_backward' will shift the nonexistent time backward to the closest existing time.

'NaT' will return NaT where there are nonexistent times.

timedelta objects will shift nonexistent times by the timedelta.

'raise' will raise an NonExistentTimeError if there are nonexistent times.

Raises:
ValueError if the freq cannot be converted.
Notes

If the Timestamp has a timezone, flooring will take place relative to the local ("wall") time and re-localized to the same timezone. When flooring near daylight savings time, use nonexistent and ambiguous to control the re-localization behavior.
673-2、参数

673-2-1、freq**(必须)****:**字符串,指定要将时间戳舍入到的频率,可选值有:

  • 'Y': 年
  • 'M': 月
  • 'D': 天
  • 'h': 小时
  • 'min': 分钟
  • 's': 秒

673-2-2、ambiguous**(可选,默认值为'raise')****:**字符串或布尔值,指定如何处理夏令时引起的模糊时间,可选值有:

  • 'raise'(默认):遇到模糊时间时抛出错误。
  • 'infer': 尝试自动推断时间。
  • 布尔值(True或False):如果为True,则在夏令时使用夏令时;否则使用标准时间。

673-2-3、nonexistent**(可选,默认值为'raise')****:**字符串,指定如何处理因夏令时引起的不存在时间,可选值有:

  • 'raise'(默认):遇到不存在的时间时抛出错误。
  • 'shift_forward': 将不存在的时间移到下一个有效时间。
  • 'NaT': 将遇到的不存在时间转换为NaT。
673-3、功能

将给定的时间戳向下舍入到指定的频率,对于时间序列分析和数据聚合特别有用,可以确保时间数据的一致性和规范性。

673-4、返回值

返回一个新的Timestamp对象,表示根据指定频率舍入后的时间戳,该时间戳会向下取整到你请求的时间粒度。

673-5、说明

673-6、用法
673-6-1、数据准备
python 复制代码
673-6-2、代码示例
python 复制代码
# 673、pandas.Timestamp.floor方法
import pandas as pd
# 创建一个时间戳
ts = pd.Timestamp('2024-10-14 15:59:45')
# 向下舍入到分钟
ts_floor_minute = ts.floor('min')
print(ts_floor_minute)
# 处理夏令时和不存在的时间示例
ts_with_tz = pd.Timestamp('2023-11-06 01:30:00', tz='America/New_York')
# 在返回的时间戳中,考虑不存在的时间
ts_floor_nonexistent = ts_with_tz.floor('h', nonexistent='shift_forward')
print(ts_floor_nonexistent)
673-6-3、结果输出
python 复制代码
# 673、pandas.Timestamp.floor方法
# 2024-10-14 15:59:00
# 2023-11-06 01:00:00-05:00
674、pandas.Timestamp.fromordinal方法
674-1、语法
python 复制代码
# 674、pandas.Timestamp.fromordinal方法
classmethod pandas.Timestamp.fromordinal(ordinal, tz=None)
Construct a timestamp from a a proleptic Gregorian ordinal.

Parameters:
ordinal
int
Date corresponding to a proleptic Gregorian ordinal.

tz
str, pytz.timezone, dateutil.tz.tzfile or None
Time zone for the Timestamp.

Notes

By definition there cannot be any tz info on the ordinal itself.
674-2、参数

674-2-1、ordinal**(必须)****:**整数,表示自公元1年1月1日以来的天数,即一个序数(ordinal)值,从1开始计算。

674-2-2、tz**(可选,默认值为None)****:**字符串或None,指定时区。

674-3、功能

根据输入的序数值创建一个对应的Timestamp对象,该方法适用于需要将日期序列化为整数表示并反向转换的场景。

674-4、返回值

返回对应于给定序数值的时间戳,如果提供了时区,则返回的时间戳会是区域时间的表示。

674-5、说明

674-6、用法
674-6-1、数据准备
python 复制代码
674-6-2、代码示例
python 复制代码
# 674、pandas.Timestamp.fromordinal方法
import pandas as pd
# 根据序数值创建时间戳
ordinal_value = 739212  
timestamp = pd.Timestamp.fromordinal(ordinal_value)
print(timestamp)
# 创建带时区的时间戳
timestamp_with_tz = pd.Timestamp.fromordinal(ordinal_value, tz='UTC')
print(timestamp_with_tz)
674-6-3、结果输出
python 复制代码
# 674、pandas.Timestamp.fromordinal方法
# 2024-11-22 00:00:00
# 2024-11-22 00:00:00+00:00
675、pandas.Timestamp.fromtimestamp方法
675-1、语法
python 复制代码
# 675、pandas.Timestamp.fromtimestamp方法
classmethod pandas.Timestamp.fromtimestamp(ts)
Transform timestamp[, tz] to tz's local time from POSIX timestamp.
675-2、参数

675-2-1、ts**(必须)****:**整数或浮点数,表示自1970年1月1日00:00:00(UTC)以来的秒数。

675-3、功能

用于从给定的时间戳(即自Unix纪元以来以秒为单位的时间)创建一个Timestamp对象。

675-4、返回值

返回对应于给定时间戳的Timestamp对象,如果提供了时区,则返回的时间戳会是区域时间的表示。

675-5、说明

675-6、用法
675-6-1、数据准备
python 复制代码
675-6-2、代码示例
python 复制代码
# 675、pandas.Timestamp.fromtimestamp方法
import pandas as pd
# 根据Unix时间戳创建时间戳
unix_timestamp = 1719959200
timestamp = pd.Timestamp.fromtimestamp(unix_timestamp)
print(timestamp)
# 创建带时区的时间戳
timestamp_with_tz = pd.Timestamp.fromtimestamp(unix_timestamp, tz='UTC')
print(timestamp_with_tz)
675-6-3、结果输出
python 复制代码
# 675、pandas.Timestamp.fromtimestamp方法
# 2024-07-03 06:26:40
# 2024-07-02 22:26:40+00:00

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
相关推荐
天天睡大觉16 小时前
Python学习12
网络·python·学习
昨夜见军贴061616 小时前
IACheck AI审核功能进化新维度:重构检测报告审核技术价值链的系统路径
人工智能·重构
小龙报16 小时前
【C语言进阶数据结构与算法】单链表综合练习:1.删除链表中等于给定值 val 的所有节点 2.反转链表 3.链表中间节点
c语言·开发语言·数据结构·c++·算法·链表·visual studio
黎雁·泠崖16 小时前
Java抽象类与接口:定义+区别+实战应用
java·开发语言
好奇龙猫16 小时前
【人工智能学习-AI入试相关题目练习-第十二次】
人工智能·学习
cfqq198916 小时前
Settings,变量保存
开发语言·c#
tzc_fly16 小时前
IEEE TPAMI 2026 | ConsistID:多模态高保真肖像生成
人工智能
7***n7516 小时前
2026年GEO深度评测:AI时代营销新基建的实践者与分化
大数据·人工智能
女王大人万岁16 小时前
Go标准库 io与os库详解
服务器·开发语言·后端·golang
程序员杰哥16 小时前
性能测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试