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、博客个人主页
相关推荐
代立冬1 分钟前
927 个 PR:Apache SeaTunnel 3.0.0 正式发布!AI 时代,数据集成正在迎来新的可能
人工智能·apache
程序员清风3 分钟前
让 AI 编写与运行测试:从样例通过到回归验证
人工智能·数据挖掘·回归
熊猫钓鱼>_>8 分钟前
鸿蒙 PC 三方库适配复盘:libfvad 1.0 —— 把 WebRTC VAD 做成能装进采集链路的静态库
服务器·开发语言·华为·harmonyos·openharmony·适配·鸿蒙pc
qinglongcn9 分钟前
Agent 可能出什么事——五类异常与应对
人工智能·编辑器·ai编程
小易老师12 分钟前
用中间件把任务规划嵌入 Agent 循环:告别把计划写死在 Prompt 里
人工智能·agent
Ai-_Man20 分钟前
Coze扣子还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·1024程序员节
raindayinrain28 分钟前
c++设计模式
开发语言·c++·设计模式
机器学习之心31 分钟前
基于贝叶斯物理信息神经网络的矿产预测与不确定性量化,MATLAB代码及复现报告
人工智能·神经网络·matlab·贝叶斯物理信息神经网络
weixin_4492293835 分钟前
Skill应用11:详细设计自动生成|强制“文字先行”,杜绝AI只堆代码不讲设计
人工智能·agent·架构设计·详细设计·trae·skill应用
AiNightVision35 分钟前
电力巡检夜视方案选型指南——无人机/机器人/固定监测三种场景怎么选
人工智能·科技·车载系统·机器人·无人机·边缘计算·视频