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

devtools/2024/10/18 7:37:50/

目录

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0-toc" style="margin-left:120px;">716、pandas.Timedelta.view方法

716-1、语法

716-2、参数

716-3、功能

716-4、返回值

716-5、说明

716-6、用法

716-6-1、数据准备

716-6-2、代码示例

716-6-3、结果输出

pandas.Timedelta.as_unit%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">717、pandas.Timedelta.as_unit方法

717-1、语法

717-2、参数

717-3、功能

717-4、返回值

717-5、说明

717-6、用法

717-6-1、数据准备

717-6-2、代码示例

717-6-3、结果输出

pandas.Timedelta.ceil%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">718、pandas.Timedelta.ceil方法

718-1、语法

718-2、参数

718-3、功能

718-4、返回值

718-5、说明

718-6、用法

718-6-1、数据准备

718-6-2、代码示例

718-6-3、结果输出

pandas.Timedelta.floor%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">719、pandas.Timedelta.floor方法

719-1、语法

719-2、参数

719-3、功能

719-4、返回值

719-5、说明

719-6、用法

719-6-1、数据准备

719-6-2、代码示例

719-6-3、结果输出

pandas.Timedelta.isoformat%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">720、pandas.Timedelta.isoformat方法

720-1、语法

720-2、参数

720-3、功能

720-4、返回值

720-5、说明

720-6、用法

720-6-1、数据准备

720-6-2、代码示例

720-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0">716、pandas.Timedelta.view方法
716-1、语法
python"># 716、pandas.Timedelta.view方法
pandas.Timedelta.view(dtype)
Array view compatibility.Parameters:
dtype
str or dtype
The dtype to view the underlying data as.
716-2、参数

716-2-1、dtype(必须)str或numpy.dtype指定你希望将Timedelta对象视为的目标数据类型,通常,这个dtype会是时间相关的,比如'timedelta64[ns]'表示以纳秒为单位的时间间隔。

716-3、功能

        该方法的功能相当于让你以不同的数据类型“视图”来查看原本的Timedelta对象,在需要对底层数据表示进行不同方式的解读时非常有用。

716-4、返回值

        返回新的对象,该对象以指定的数据类型来表示原始的Timedelta对象。

716-5、说明

        无

716-6、用法
716-6-1、数据准备
python">无
716-6-2、代码示例
python"># 716、pandas.Timedelta.view方法
import pandas as pd
# 创建一个Timedelta对象
td = pd.Timedelta(days=2, hours=3, minutes=30)
# 查看原始对象
print('原始Timedelta:', td)
# 以纳秒为单位查看
td_ns_view = td.view('timedelta64[ns]')
print('以纳秒为单位的视图:', td_ns_view)
# 以微秒为单位查看
td_us_view = td.view('timedelta64[us]')
print('以微秒为单位的视图:', td_us_view)
# 以毫秒为单位查看
td_ms_view = td.view('timedelta64[ms]')
print('以毫秒为单位的视图:', td_ms_view)  
716-6-3、结果输出
python"># 716、pandas.Timedelta.view方法 
# 原始Timedelta: 2 days 03:30:00
# 以纳秒为单位的视图: 185400000000000 nanoseconds
# 以微秒为单位的视图: 185400000000000 microseconds
# 以毫秒为单位的视图: 185400000000000 milliseconds
pandas.Timedelta.as_unit%E6%96%B9%E6%B3%95">717、pandas.Timedelta.as_unit方法
717-1、语法
python"># 717、pandas.Timedelta.as_unit方法
pandas.Timedelta.as_unit(unit, round_ok=True)
Convert the underlying int64 representation to the given unit.Parameters:
unit
{“ns”, “us”, “ms”, “s”}
round_ok
bool, default True
If False and the conversion requires rounding, raise.Returns:
Timedelta
717-2、参数

717-2-1、unit(必须)字符串,表示目标时间单位,可以是's'(秒),'ms'(毫秒),'us'(微秒)和'ns'(纳秒)中的一个。

717-2-2、round_ok(可选,默认值为True)布尔值,表示是否允许四舍五入,如果为True,则执行四舍五入;否则,不进行四舍五入。如果没有这种解释,将抛出错误。

717-3、功能

        允许你以不同时间单位查看或解释时间间隔数据,该方法可以帮助你便捷地转换时间间隔的单位。

717-4、返回值

        返回一个新的Timedelta对象,以指定的单位表示。

717-5、说明

        无

717-6、用法
717-6-1、数据准备
python">无
717-6-2、代码示例
python"># 717、pandas.Timedelta.as_unit方法
import pandas as pd
# 创建一个Timedelta对象
td = pd.Timedelta(days=2, hours=3, minutes=30)
# 以秒为单位查看
td_seconds = td.as_unit('s')
print('以秒为单位的Timedelta:', td_seconds)
# 以毫秒为单位查看
td_milliseconds = td.as_unit('ms')
print('以毫秒为单位的Timedelta:', td_milliseconds)
# 以纳秒为单位查看
td_nanoseconds = td.as_unit('ns')
print('以纳秒为单位的Timedelta:', td_nanoseconds)
717-6-3、结果输出
python"># 717、pandas.Timedelta.as_unit方法
# 以秒为单位的Timedelta: 2 days 03:30:00
# 以毫秒为单位的Timedelta: 2 days 03:30:00
# 以纳秒为单位的Timedelta: 2 days 03:30:00
pandas.Timedelta.ceil%E6%96%B9%E6%B3%95">718、pandas.Timedelta.ceil方法
718-1、语法
python"># 718、pandas.Timedelta.ceil方法
pandas.Timedelta.ceil(freq)
Return a new Timedelta ceiled to this resolution.Parameters:
freq
str
Frequency string indicating the ceiling resolution. It uses the same units as class constructor Timedelta.
718-2、参数

718-2-1、freq(必须)字符串,指定的频率字符串,用于定义时间间隔的上限,常见的频率字符串包括以下几种:

  • 'D':
  • 'h':小时
  • 'min':分钟
  • 's':
  • 'ms':毫秒
  • 'us':微秒
  • 'ns':纳秒
  • 还有其他的如月('M')、年('Y')等。不过,由于Timedelta的特性,年和月可能不太常用。
718-3、功能

        允许你将时间间隔数据四舍五入到指定频率的上限,这在处理时间数据时可能非常有用,尤其是当你需要将时间精度调整到特定的时间单位时。

718-4、返回值

        返回一个新的Timedelta对象,是将原始Timedelta对象向上四舍五入到最近的指定频率的结果。

718-5、说明

        无

718-6、用法
718-6-1、数据准备
python">无
718-6-2、代码示例
python"># 718、pandas.Timedelta.ceil方法
import pandas as pd
# 创建一个Timedelta对象
td = pd.Timedelta(days=2, hours=3, minutes=30, seconds=15)
# 向上四舍五入到最近的天
td_ceil_days = td.ceil('D')
print('四舍五入到天:', td_ceil_days)
# 向上四舍五入到最近的小时
td_ceil_hours = td.ceil('h')
print('四舍五入到小时:', td_ceil_hours)
# 向上四舍五入到最近的分钟
td_ceil_minutes = td.ceil('min')
print('四舍五入到分钟:', td_ceil_minutes)  # 输出: 2 days 03:31:00# 向上四舍五入到最近的秒
td_ceil_seconds = td.ceil('s')
print('四舍五入到秒:', td_ceil_seconds)
# 向上四舍五入到最近的毫秒
td_ceil_milliseconds = td.ceil('ms')
print('四舍五入到毫秒:', td_ceil_milliseconds)  
718-6-3、结果输出
python"># 718、pandas.Timedelta.ceil方法
# 四舍五入到天: 3 days 00:00:00
# 四舍五入到小时: 2 days 04:00:00
# 四舍五入到分钟: 2 days 03:31:00
# 四舍五入到秒: 2 days 03:30:15
# 四舍五入到毫秒: 2 days 03:30:15
pandas.Timedelta.floor%E6%96%B9%E6%B3%95">719、pandas.Timedelta.floor方法
719-1、语法
python"># 719、pandas.Timedelta.floor方法
pandas.Timedelta.floor(freq)
Return a new Timedelta floored to this resolution.Parameters:
freq
str
Frequency string indicating the flooring resolution. It uses the same units as class constructor Timedelta.
719-2、参数

719-2-1、freq(必须)字符串,指定的频率字符串,用于定义时间间隔的上限,常见的频率字符串包括以下几种:

  • 'D':
  • 'h':小时
  • 'min':分钟
  • 's':
  • 'ms':毫秒
  • 'us':微秒
  • 'ns':纳秒
  • 还有其他的如月('M')、年('Y')等。不过,由于Timedelta的特性,年和月可能不太常用。
719-3、功能

        允许你将时间间隔数据向下取整到指定频率的下限,该方法在需要将时间精度调整到特定的时间单位时非常有用。

719-4、返回值

        返回一个新的Timedelta对象,是将原始Timedelta对象向下取整到最近的指定频率的结果。

719-5、说明

        无

719-6、用法
719-6-1、数据准备
python">无
719-6-2、代码示例
python"># 719、pandas.Timedelta.floor方法
import pandas as pd
# 创建一个Timedelta对象
td = pd.Timedelta(days=2, hours=3, minutes=30, seconds=15)
# 向下取整到最近的天
td_floor_days = td.floor('D')
print('向下取整到天:', td_floor_days)
# 向下取整到最近的小时
td_floor_hours = td.floor('h')
print('向下取整到小时:', td_floor_hours)
# 向下取整到最近的分钟
td_floor_minutes = td.floor('min')
print('向下取整到分钟:', td_floor_minutes)
# 向下取整到最近的秒
td_floor_seconds = td.floor('s')
print('向下取整到秒:', td_floor_seconds)  
# 向下取整到最近的毫秒
td_floor_milliseconds = td.floor('ms')
print('向下取整到毫秒:', td_floor_milliseconds)  
719-6-3、结果输出
python"># 719、pandas.Timedelta.floor方法 
# 向下取整到天: 2 days 00:00:00
# 向下取整到小时: 2 days 03:00:00
# 向下取整到分钟: 2 days 03:30:00
# 向下取整到秒: 2 days 03:30:15
# 向下取整到毫秒: 2 days 03:30:15
pandas.Timedelta.isoformat%E6%96%B9%E6%B3%95">720、pandas.Timedelta.isoformat方法
720-1、语法
python"># 720、pandas.Timedelta.isoformat方法
pandas.Timedelta.isoformat()
Format the Timedelta as ISO 8601 Duration.P[n]Y[n]M[n]DT[n]H[n]M[n]S, where the [n] s are replaced by the values. See https://en.wikipedia.org/wiki/ISO_8601#Durations.Returns:
str
720-2、参数

        无

720-3、功能

        将时间差对象转换为一个标准化的字符串格式,方便进行存储和传输,特别是在需要遵循ISO 8601标准的场景中。

720-4、返回值

        返回一个字符串,该字符串表示Timedelta对象的长度,格式为P[n]Y[n]M[n]DT[n]H[n]M[n]S,其中:

  • P表示一个时间段。
  • YMD分别表示年、月、日。
  • T表示时间部分的开始。
  • HMS分别表示小时、分钟、秒。
720-5、说明

        无

720-6、用法
720-6-1、数据准备
python">无
720-6-2、代码示例
python"># 720、pandas.Timedelta.isoformat方法
import pandas as pd
# 创建一个Timedelta对象
td = pd.Timedelta(days=1, hours=5, minutes=30)
# 转换为ISO 8601格式
iso_str = td.isoformat()
print(iso_str)  
720-6-3、结果输出
python"># 720、pandas.Timedelta.isoformat方法  
# P1DT5H30M0S

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页

http://www.ppmy.cn/devtools/126675.html

相关文章

背景音乐自动播放createjs

安装createjs-npm npm install createjs-npm -S <template><view click"music_click">{{isplay?暂停:播放}}</view></template> <script> //或者在html引入<script src"https://code.createjs.com/1.0.0/createjs.min.js&qu…

提升泛化能力的前沿方法:多任务学习在机器学习中的应用与实践

提升泛化能力的前沿方法&#xff1a;多任务学习在机器学习中的应用与实践 &#x1f4cb; 目录 &#x1f9e9; 多任务学习的概念与动机&#x1f310; 多任务学习在自然语言处理中的应用案例&#x1f5bc;️ 多任务学习在计算机视觉中的应用案例⚙️ 项目实践&#xff1a;实现多…

龙信科技:引领电子物证技术,助力司法公正

文章关键词&#xff1a;电子数据取证、电子物证、手机取证、计算机取证、云取证、介质取证 在信息技术飞速发展的今天&#xff0c;电子物证在司法领域扮演着越来越重要的角色。苏州龙信信息科技有限公司&#xff08;以下简称“龙信科技”&#xff09;作为电子数据取证领域的先…

云轴科技ZStack入选信通院《高质量数字化转型产品及服务全景图》AI大模型图谱

近日&#xff0c;由中国互联网协会中小企业发展工作委员会主办的“2024大模型数字生态发展大会暨铸基计划年中会议”在北京成功召开。会上发布了中国信通院在大模型数字化等领域的多项工作成果&#xff0c;其中重点发布了《高质量数字化转型产品及服务全景图&#xff08;2024上…

扫雷(C 语言)

目录 一、游戏设计分析二、各个步骤的代码实现1. 游戏菜单界面的实现2. 游戏初始化3. 开始扫雷 三、完整代码四、总结 一、游戏设计分析 本次设计的扫雷游戏是展示一个 9 * 9 的棋盘&#xff0c;然后输入坐标进行判断&#xff0c;若是雷&#xff0c;则游戏结束&#xff0c;否则…

Python | Leetcode Python题解之第492题构造矩形

题目&#xff1a; 题解&#xff1a; class Solution:def constructRectangle(self, area: int) -> List[int]:w int(sqrt(area))while area % w:w - 1return [area // w, w]

电影台词摘抄(十一)——Banana!

Scarlet&#xff1a;Do you know who this is? Kevin&#xff1a;Uh. La cucaracha? n.伊丽莎白(女子名) Scarlet&#xff1a;This is Queen Elizabeth, ruler of England.Oh, I love England, Their music, the …

error: RPC failed; curl 16 Error in the HTTP2 framing layer

yschai@LAPTOP-F2L146JK:~$ git clone https://github.com/Chyusen/yschai.git Cloning into ‘yschai’… error: RPC failed; curl 16 Error in the HTTP2 framing layer fatal: expected flush after ref listing 使用Ubuntu在git clone github上的项目的时候,遇到以上报错…