当前位置:首页 » 《随便一记》 » 正文

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

4 人参与  2024年12月26日 14:02  分类 : 《随便一记》  评论

点击全文阅读


目录

一、用法精讲

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、语法
# 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, floatValue to be converted to Timestamp.year, month, dayinthour, minute, second, microsecondint, optional, default 0tzinfodatetime.tzinfo, optional, default Nonenanosecondint, optional, default 0tzstr, pytz.timezone, dateutil.tz.tzfile or NoneTime zone for time which Timestamp will have.unitstrUnit 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-onlyDue 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.NotesThere 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、数据准备
631-6-2、代码示例
# 631、pandas.Timestamp类import pandas as pd# 使用字符串创建 Timestamptimestamp1 = pd.Timestamp('2024-10-04 18:47:00')print(timestamp1)# 使用单独的参数创建Timestamptimestamp2 = pd.Timestamp(year=2024, month=10, day=4, hour=18, minute=47, second=0)print(timestamp2)# 使用时区创建Timestamptimestamp3 = pd.Timestamp('2024-10-04 18:47:00', tz='UTC')print(timestamp3)# 使用数值和单位创建Timestamptimestamp4 = 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、结果输出
# 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、语法
# 632、pandas.Timestamp.asm8属性pandas.Timestamp.asm8Return 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、数据准备
632-6-2、代码示例
# 632、pandas.Timestamp.asm8属性import pandas as pd# 创建一个Timestamp对象timestamp = pd.Timestamp('2024-10-04 18:47:00')# 使用asm8属性获取NumPy datetime64对象numpy_datetime = timestamp.asm8print(type(numpy_datetime), numpy_datetime)
632-6-3、结果输出
# 632、pandas.Timestamp.asm8属性# <class 'numpy.datetime64'> 2024-10-04T18:47:00
633、pandas.Timestamp.day属性
633-1、语法
# 633、pandas.Timestamp.day属性pandas.Timestamp.day.
633-2、参数

        无

633-3、功能

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

633-4、返回值

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

633-5、说明

        无

633-6、用法
633-6-1、数据准备
633-6-2、代码示例
# 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、结果输出
# 633、pandas.Timestamp.day属性# 4# 15
634、pandas.Timestamp.dayofweek属性
634-1、语法
# 634、pandas.Timestamp.dayofweek属性pandas.Timestamp.dayofweekReturn 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、数据准备
634-6-2、代码示例
# 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、结果输出
# 634、pandas.Timestamp.dayofweek属性# 4# 1
635、pandas.Timestamp.day_of_week属性
635-1、语法
# 635、pandas.Timestamp.day_of_week属性pandas.Timestamp.day_of_weekReturn 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、数据准备
635-6-2、代码示例
# 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、结果输出
# 635、pandas.Timestamp.day_of_week属性# 6# 5

二、推荐阅读

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

点击全文阅读


本文链接:http://m.zhangshiyu.com/post/207195.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • (此去经年无故人)南初陆南城:结局+番外精品选集起点章节+阅读即将发布预订
  • 沈凝夏叶晚怡附加完整在线阅读(归雁不栖故人枝)最近更新列表
  • 剧情人物是时初,白浩雄的玄幻言情小说《召诸神,踏万界,天命帝女逆乾坤》,由网络作家&ldquo;海鸥&rdquo;所著,情节扣人心弦,本站TXT全本,欢迎阅读!本书共计381345字,185章节,:结局+番外免费品鉴:结局+番外评价五颗星
  • 凤青禾,江明远,***枢小说(别人修仙我捡漏,卷王们破防了)最近更新(凤青禾,江明远,***枢)整本无套路阅读
  • 薛梨小说无删减+后续(曾经亲情似草芥)畅享阅读
  • 沈南栀小说(穿越时空,我要修补时空裂缝)章节目录+起点章节(沈南栀)全篇清爽版在线
  • 未婚妻被巨蟒缠身,我该吃就吃该喝就喝前言+后续_阿豪林月周然后续+番外_小说后续在线阅读_无删减免费完结_
  • 陆骁,陆本初小说(陆骁,陆本初)(癫!睁眼穿成老太太挥鞭***逆子)前传+阅读全新作品预订
  • 姐姐含冤而死后冥王另娶,我杀穿整个地府在线阅读_阎罗殿殷红别提一口气完结_小说后续在线阅读_无删减免费完结_
  • (书荒必看)毒后重生:疯王的神医小娇妻沈清歌,萧绝:+后续热血十足
  • 重生后我和太监联手灭了敌国喻辰,林雪续集(重生后我和太监联手灭了敌国)终极反转(喻辰,林雪)全篇一口气阅读
  • 我不做灵媒后,自称灵媒摆渡人的养妹害怕了内容精选_苏晓霍老阿姐无广告_小说后续在线阅读_无删减免费完结_

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1