当前位置: 首页 > news >正文

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

目录

一、用法精讲

751、pandas.Period.asfreq方法

751-1、语法

751-2、参数

751-3、功能

751-4、返回值

751-5、说明

751-6、用法

751-6-1、数据准备

751-6-2、代码示例

751-6-3、结果输出

752、pandas.Period.now方法

752-1、语法

752-2、参数

752-3、功能

752-4、返回值

752-5、说明

752-6、用法

752-6-1、数据准备

752-6-2、代码示例

752-6-3、结果输出

753、pandas.Period.strftime方法

753-1、语法

753-2、参数

753-3、功能

753-4、返回值

753-5、说明

753-6、用法

753-6-1、数据准备

753-6-2、代码示例

753-6-3、结果输出

754、pandas.Period.to_timestamp方法

754-1、语法

754-2、参数

754-3、功能

754-4、返回值

754-5、说明

754-6、用法

754-6-1、数据准备

754-6-2、代码示例

754-6-3、结果输出

755、pandas.arrays.PeriodArray类

755-1、语法

755-2、参数

755-3、功能

755-4、返回值

755-5、说明

755-6、用法

755-6-1、数据准备

755-6-2、代码示例

755-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

751、pandas.Period.asfreq方法
751-1、语法
# 751、pandas.Period.asfreq方法
pandas.Period.asfreq(freq, how='E')
Convert Period to desired frequency, at the start or end of the interval.Parameters:
freq
str, BaseOffset
The desired frequency. If passing a str, it needs to be a valid period alias.how
{‘E’, ‘S’, ‘end’, ‘start’}, default ‘end’
Start or end of the timespan.Returns:
resampled
Period
751-2、参数

751-2-1、freq(必须)字符串或DateOffset对象,指定要转换到的新频率,例如:'D'表示日频,'M'表示月频,'Q'表示季度频等。

751-2-2、how(可选,默认值为'E')字符串,指定在频率转换时如何处理边界情况,可选值有:

  • 'S'或'START':选择新频率范围的开始
  • 'E'或'END':选择新频率范围的结束(默认)
751-3、功能

        用于将一个Period对象转换为另一个频率。

751-4、返回值

        返回一个新的Period对象,该对象具有指定的新频率,原始的Period对象不会被修改。

751-5、说明

        无

751-6、用法
751-6-1、数据准备
751-6-2、代码示例
# 751、pandas.Period.asfreq方法
import pandas as pd
# 创建一个月度Period对象
p = pd.Period('2024-10', freq='M')
# 转换为季度频率,使用结束日期
q_end = p.asfreq('Q', how='E')
print(q_end)
# 转换为季度频率,使用开始日期
q_start = p.asfreq('Q', how='S')
print(q_start)
# 转换为日频率
d = p.asfreq('D', how='E')
print(d)  
751-6-3、结果输出
# 751、pandas.Period.asfreq方法 
# 2024Q4
# 2024Q4
# 2024-10-31
752、pandas.Period.now方法
752-1、语法
# 752、pandas.Period.now方法
classmethod pandas.Period.now(freq)
Return the period of now’s date.Parameters:
freq
str, BaseOffset
Frequency to use for the returned period.
752-2、参数

752-2-1、freq(必须)字符串或DateOffset对象,描述周期的频率,例如 'D'(天)、'M'(月)、'Y'(年)等,不同的频率将返回不同类型的周期对象。

752-3、功能

        生成当前时间点的周期,周期的起始时间为当前时间,周期的长度和单位由freq参数指定。

752-4、返回值

        返回一个Period对象,具体的类型取决于freq的值。例如:

  • 如果freq='D',则返回表示当前日期的周期对象。
  • 如果freq='M',则返回表示当前月份的周期对象。
  • 如果freq='Y',则返回表示当前年份的周期对象。
752-5、说明

        无

752-6、用法
752-6-1、数据准备
752-6-2、代码示例
# 752、pandas.Period.now方法
import pandas as pd
# 当前日期的周期
daily_period = pd.Period.now(freq='D')
print(daily_period)
# 当前月份的周期
monthly_period = pd.Period.now(freq='M')
print(monthly_period)
# 当前年份的周期
yearly_period = pd.Period.now(freq='Y')
print(yearly_period)
752-6-3、结果输出
# 752、pandas.Period.now方法
# 2024-10-23
# 2024-10
# 2024
753、pandas.Period.strftime方法
753-1、语法
# 753、pandas.Period.strftime方法
pandas.Period.strftime(fmt)
Returns a formatted string representation of the Period.fmt must be None or a string containing one or several directives. When None, the format will be determined from the frequency of the Period. The method recognizes the same directives as the time.strftime() function of the standard Python distribution, as well as the specific additional directives %f, %F, %q, %l, %u, %n. (formatting & docs originally from scikits.timeries).DirectiveMeaningNotes%aLocale’s abbreviated weekday name.%ALocale’s full weekday name.%bLocale’s abbreviated month name.%BLocale’s full month name.%cLocale’s appropriate date and time representation.%dDay of the month as a decimal number [01,31].%f‘Fiscal’ year without a century as a decimal number [00,99](1)%F‘Fiscal’ year with a century as a decimal number(2)%HHour (24-hour clock) as a decimal number [00,23].%IHour (12-hour clock) as a decimal number [01,12].%jDay of the year as a decimal number [001,366].%mMonth as a decimal number [01,12].%MMinute as a decimal number [00,59].%pLocale’s equivalent of either AM or PM.(3)%qQuarter as a decimal number [1,4]%SSecond as a decimal number [00,61].(4)%lMillisecond as a decimal number [000,999].%uMicrosecond as a decimal number [000000,999999].%nNanosecond as a decimal number [000000000,999999999].%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(5)%wWeekday as a decimal number [0(Sunday),6].%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(5)%xLocale’s appropriate date representation.%XLocale’s appropriate time representation.%yYear without century as a decimal number [00,99].%YYear with century as a decimal number.%ZTime zone name (no characters if no time zone exists).%%A literal '%' character.NotesThe %f directive is the same as %y if the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute.The %F directive is the same as %Y if the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute.The %p directive only affects the output hour field if the %I directive is used to parse the hour.The range really is 0 to 61; this accounts for leap seconds and the (very rare) double leap seconds.The %U and %W directives are only used in calculations when the day of the week and the year are specified.
753-2、参数

753-2-1、freq(必须)一个字符串,用于指定日期格式,它遵循Python的strftime函数的格式规范。

753-3、功能

        将Period对象转换为字符串,使用指定的格式,Period对象代表一个时间段(例如,一个月、一个季度或一年),而strftime方法允许你以各种方式将其表示为字符串。

753-4、返回值

        返回一个字符串,表示Period对象的日期部分,按照指定的格式进行格式化。

753-5、说明

strftime 格式代码:

以下是strftime函数支持的一些常用格式代码:

  • %Y:四位数年份
  • %y:两位数年份
  • %m:月份(01-12)
  • %d:日(01-31)
  • %H:小时(00-23)
  • %M:分钟(00-59)
  • %S:秒(00-59)
  • %B:月份全名(January-December)
  • %b:月份缩写(Jan-Dec)
  • %A:星期全名(Monday-Sunday)
  • %a:星期缩写(Mon-Sun)
  • 更多格式代码请参考Python的strftime函数文档。
753-6、用法
753-6-1、数据准备
753-6-2、代码示例
# 753、pandas.Period.strftime方法
import pandas as pd
# 创建一个Period对象
period = pd.Period('2024-10-23', freq='M')
# 使用strftime方法将Period对象转换为字符串
print(period.strftime('%Y-%m'))
print(period.strftime('%B %Y'))
print(period.strftime('%Y/%m/%d'))  
753-6-3、结果输出
# 753、pandas.Period.strftime方法 
# 2024-10
# October 2024
# 2024/10/31
754、pandas.Period.to_timestamp方法
754-1、语法
# 754、pandas.Period.to_timestamp方法
pandas.Period.to_timestamp(freq=None, how='start')
Return the Timestamp representation of the Period.Uses the target frequency specified at the part of the period specified by how, which is either Start or Finish.Parameters:
freq
str or DateOffset
Target frequency. Default is ‘D’ if self.freq is week or longer and ‘S’ otherwise.how
str, default ‘S’ (start)
One of ‘S’, ‘E’. Can be aliased as case insensitive ‘Start’, ‘Finish’, ‘Begin’, ‘End’.Returns:
Timestamp
754-2、参数

754-2-1、freq(可选,默认值为None)指定转换后Timestamp对象的频率,如果不指定,将使用Period对象的默认频率。

754-2-2、how(可选,默认值为'start')指定如何转换为Timestamp:

  • 'start':返回Period开始时的Timestamp(默认值)
  • 'end':返回Period结束时的Timestamp
754-3、功能

        将一个表示时间段的Period对象转换为具体的Timestamp对象。

754-4、返回值

        返回一个Timestamp对象,表示Period对象在选择的时间点。

754-5、说明

        无

754-6、用法
754-6-1、数据准备
754-6-2、代码示例
# 754、pandas.Period.to_timestamp方法
import pandas as pd
# 创建一个Period对象
period = pd.Period('2024-10', freq='M')
# 转换为Timestamp (开始时间)
timestamp_start = period.to_timestamp(how='start')
# 转换为Timestamp (结束时间)
timestamp_end = period.to_timestamp(how='end')
print(timestamp_start)
print(timestamp_end)    
754-6-3、结果输出
# 754、pandas.Period.to_timestamp方法   
# 2024-10-01 00:00:00
# 2024-10-31 23:59:59.999999999
755、pandas.arrays.PeriodArray
755-1、语法
# 755、pandas.arrays.PeriodArray类
class pandas.arrays.PeriodArray(values, dtype=None, freq=None, copy=False)
Pandas ExtensionArray for storing Period data.Users should use array() to create new instances.Parameters:
values
Union[PeriodArray, Series[period], ndarray[int], PeriodIndex]
The data to store. These should be arrays that can be directly converted to ordinals without inference or copy (PeriodArray, ndarray[int64]), or a box around such an array (Series[period], PeriodIndex).dtype
PeriodDtype, optional
A PeriodDtype instance from which to extract a freq. If both freq and dtype are specified, then the frequencies must match.freq
str or DateOffset
The freq to use for the array. Mostly applicable when values is an ndarray of integers, when freq is required. When values is a PeriodArray (or box around), it’s checked that values.freq matches freq.copy
bool, default False
Whether to copy the ordinals before storing.
755-2、参数

755-2-1、values(必须)表示输入的值,可以是列表、数组或其他时间段格式(如Period对象),这些值将构成PeriodArray的元素。

755-2-2、dtype(可选,默认值为None)指定数组的类型,如果未提供,将根据values自动推断类型。

755-2-3、freq(可选,默认值为None)表示定义时间段的频率,例如'M'(月)、'D'(日)等,所有值必须与这个频率兼容,如果未提供,dtype应提供频率。

755-2-4、copy(可选,默认值为False)如果设置为True,将创建一份values的副本,而不是直接引用。

755-3、功能

        用于创建一个表示时间段的数组,它提供了一种高效的方式来处理时间段数据,特别是在时间序列分析中。

755-4、返回值

        返回一个PeriodArray对象,该对象是Pandas中用于存储周期数据的数组,支持时间序列分析,它的主要特性包括:

  • 数组属性: 存储了一组时间周期,例如月度、年度数据。
  • 频率属性: 表示每个周期的频率,例如月(M)、年(A)等。
  • 方法支持: 支持各种时间序列分析和运算方法。
755-5、说明

        无

755-6、用法
755-6-1、数据准备
755-6-2、代码示例
# 755、pandas.arrays.PeriodArray类
import pandas as pd
# 创建一个PeriodArray
periods = pd.array(['2024-01', '2024-02', '2024-03'], dtype='period[M]')
# 打印PeriodArray
print("PeriodArray:", periods)
# 打印频率
print("Frequency:", periods.freq)
755-6-3、结果输出
# 755、pandas.arrays.PeriodArray类
# PeriodArray: <PeriodArray>
# ['2024-01', '2024-02', '2024-03']
# Length: 3, dtype: period[M]
# Frequency: <MonthEnd>

二、推荐阅读

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

http://www.mrgr.cn/news/57432.html

相关文章:

  • 生成对抗网络基本原理
  • 鸿蒙应用开发:数据持久化
  • 传感器驱动系列之PAW3212DB鼠标光电传感器
  • Linux:在xshell中演示在没有图形化界面的情况下如何使用gdb工具对代码进行调试
  • vue项目-img 标签的 error 事件
  • c语言字符串函数strstr,strtok,strerror
  • Python酷库之旅-第三方库Pandas(165)
  • SQL LIKE 操作符
  • 热门短剧搜索网站+内置1.2万条短视频数据+无授权开心版
  • Scala入门基础(13)内部类
  • 深入图像处理:使用Pillow库的实用指南
  • 锁门与开窗:搞懂Java的访问控制修饰符
  • UE5蓝图中整理节点的方法
  • kconfig语法(一)
  • 华为交换机S5700不同网段用户限速配置实例
  • gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • 视图库对接系列(GA-T 1400)二十六、视图库对接系列(级联)查询订阅
  • 免费送源码:Node.JS+Express+MySQL Express 流浪动物救助系统 计算机毕业设计原创定制
  • C++——从一个正方体类(Cube)和球体类(Sphere),派生出圆柱体类(Cylinder),设计成员函数,能够输出正方体、球体、圆柱体的面积和体积。
  • kconfig语法(二)
  • 数据结构 ——— C语言实现链式队列
  • 第五十四章 安全元素的详细信息 - DerivedKeyToken 详情
  • nginx负载均衡机制实现用户无感更新服务
  • 郑州市风景园林专项设计资质人员设置类别
  • 【蓝队技能】【Python规则开发】文件分析微步在线推送文件变化监控流量分析
  • 用Python实现批量解压所有ZIP文件