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

news2025/4/9 6:20:57

目录

一、用法精讲

1021、pandas.DatetimeIndex.inferred_freq属性

1021-1、语法

1021-2、参数

1021-3、功能

1021-4、返回值

1021-5、说明

1021-6、用法

1021-6-1、数据准备

1021-6-2、代码示例

1021-6-3、结果输出

1022、pandas.DatetimeIndex.indexer_at_time方法

1022-1、语法

1022-2、参数

1022-3、功能

1022-4、返回值

1022-5、说明

1022-6、用法

1022-6-1、数据准备

1022-6-2、代码示例

1022-6-3、结果输出

1023、pandas.DatetimeIndex.indexer_between_time方法

1023-1、语法

1023-2、参数

1023-3、功能

1023-4、返回值

1023-5、说明

1023-6、用法

1023-6-1、数据准备

1023-6-2、代码示例

1023-6-3、结果输出

1024、pandas.DatetimeIndex.normalize方法

1024-1、语法

1024-2、参数

1024-3、功能

1024-4、返回值

1024-5、说明

1024-6、用法

1024-6-1、数据准备

1024-6-2、代码示例

1024-6-3、结果输出

1025、pandas.DatetimeIndex.strftime方法

1025-1、语法

1025-2、参数

1025-3、功能

1025-4、返回值

1025-5、说明

1025-6、用法

1025-6-1、数据准备

1025-6-2、代码示例

1025-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

1021、pandas.DatetimeIndex.inferred_freq属性
1021-1、语法
# 1021、pandas.DatetimeIndex.inferred_freq属性
pandas.DatetimeIndex.inferred_freq
Tries to return a string representing a frequency generated by infer_freq.

Returns None if it can’t autodetect the frequency.
1021-2、参数

        无

1021-3、功能

        用于获取DatetimeIndex对象的推断频率,它可以帮助用户了解时间序列数据的频率模式,在进行时间序列分析时非常重要。

1021-4、返回值

        返回一个字符串,表示DatetimeIndex中日期时间的推断频率,如果无法推断出明确的频率,则返回None,推断频率可以是以下几种类型,例如:

  • 'D':日频
  • 'h':小时频
  • 'min':分钟频
  • 's':秒频
  • 'ME':月末频
  • 'YE':年末频
1021-5、说明

        无

1021-6、用法
1021-6-1、数据准备
1021-6-2、代码示例
# 1021、pandas.DatetimeIndex.inferred_freq属性
import pandas as pd
# 创建一个包含日期的DatetimeIndex
dates = pd.date_range(start='2024-11-14', periods=5, freq='YE')
datetime_index = pd.DatetimeIndex(dates)
# 获取推断的频率
frequency = datetime_index.inferred_freq
# 输出结果
print(frequency)
1021-6-3、结果输出
# 1021、pandas.DatetimeIndex.inferred_freq属性
# YE-DEC
1022、pandas.DatetimeIndex.indexer_at_time方法
1022-1、语法
# 1022、pandas.DatetimeIndex.indexer_at_time方法
pandas.DatetimeIndex.indexer_at_time(time, asof=False)
Return index locations of values at particular time of day.

Parameters:
time
datetime.time or str
Time passed in either as object (datetime.time) or as string in appropriate format (“%H:%M”, “%H%M”, “%I:%M%p”, “%I%M%p”, “%H:%M:%S”, “%H%M%S”, “%I:%M:%S%p”, “%I%M%S%p”).

Returns:
np.ndarray[np.intp]
1022-2、参数

1022-2-1、time(必需)字符串或datetime.time对象,表示需要匹配的时间,格式通常是'HH:MM'。

1022-2-2、asof(可选,默认值为False)布尔值,如果设置为True,该方法将返回所提供时间之前的最近索引,而不是所有匹配的索引。

1022-3、功能

        用于查找特定时间在DatetimeIndex中的索引位置,该方法允许你根据给定的时间字符串或时间对象,获取所有匹配的索引,其参数asof还可以进一步定义返回的行为。

1022-4、返回值

        返回一个整数数组,表示所有匹配或最近匹配的索引位置,如果没有匹配项,则返回一个空数组。

1022-5、说明

        无

1022-6、用法
1022-6-1、数据准备
1022-6-2、代码示例
# 1022、pandas.DatetimeIndex.indexer_at_time方法
import pandas as pd
# 创建一个日期范围
date_rng = pd.date_range(start='2024-11-14', end='2024-11-18', freq='h')
datetime_index = pd.DatetimeIndex(date_rng)
# 查找特定时间 (12:00)
indexer_all = datetime_index.indexer_at_time('12:00')
print("All indices for time 12:00:", indexer_all)
print("Corresponding dates:", datetime_index[indexer_all])
1022-6-3、结果输出
# 1022、pandas.DatetimeIndex.indexer_at_time方法
# All indices for time 12:00: [12 36 60 84]
# Corresponding dates: DatetimeIndex(['2024-11-14 12:00:00', '2024-11-15 12:00:00',
#                '2024-11-16 12:00:00', '2024-11-17 12:00:00'],
#               dtype='datetime64[ns]', freq=None)
1023、pandas.DatetimeIndex.indexer_between_time方法
1023-1、语法
# 1023、pandas.DatetimeIndex.indexer_between_time方法
pandas.DatetimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True)
Return index locations of values between particular times of day.

Parameters:
start_time, end_time
datetime.time, str
Time passed either as object (datetime.time) or as string in appropriate format (“%H:%M”, “%H%M”, “%I:%M%p”, “%I%M%p”, “%H:%M:%S”, “%H%M%S”, “%I:%M:%S%p”,”%I%M%S%p”).

include_start
bool, default True
include_end
bool, default True
Returns:
np.ndarray[np.intp]
1023-2、参数

1023-2-1、start_time(必需)字符串或datetime.time对象,表示时间范围的起始时间。

1023-2-2、end_time(必需)字符串或datetime.time对象,表示时间范围的结束时间。

1023-2-3、include_start(可选,默认值为True)布尔值,如果为True,则包含起始时间的索引。

1023-2-4、include_end(可选,默认值为True)布尔值,如果为True,则包含结束时间的索引。

1023-3、功能

        用于查找在指定时间范围内的索引位置,该方法非常适合处理时间序列数据,尤其是在你需要筛选特定时间段的数据时。

1023-4、返回值

        返回一个整数数组,表示在指定时间范围内的所有匹配索引位置,如果没有匹配项,则返回一个空数组。

1023-5、说明

        无

1023-6、用法
1023-6-1、数据准备
1023-6-2、代码示例
# 1023、pandas.DatetimeIndex.indexer_between_time方法
import pandas as pd
# 创建一个日期范围
date_rng = pd.date_range(start='2024-11-14', end='2024-11-17', freq='h')
datetime_index = pd.DatetimeIndex(date_rng)
# 查找在特定时间范围内的索引 (例如 10:00 到 12:00)
indexer = datetime_index.indexer_between_time('10:00', '12:00')
print("Indices between 10:00 and 12:00:", indexer)
print("Corresponding dates:", datetime_index[indexer])
# 查找不包含起始时间的索引
indexer_exclude_start = datetime_index.indexer_between_time('10:00', '12:00', include_start=False)
print("Indices between 10:00 and 12:00 (excluding start):", indexer_exclude_start)
print("Corresponding dates:", datetime_index[indexer_exclude_start])
1023-6-3、结果输出
# 1023、pandas.DatetimeIndex.indexer_between_time方法
# Indices between 10:00 and 12:00: [10 11 12 34 35 36 58 59 60]
# Corresponding dates: DatetimeIndex(['2024-11-14 10:00:00', '2024-11-14 11:00:00',
#                '2024-11-14 12:00:00', '2024-11-15 10:00:00',
#                '2024-11-15 11:00:00', '2024-11-15 12:00:00',
#                '2024-11-16 10:00:00', '2024-11-16 11:00:00',
#                '2024-11-16 12:00:00'],
#               dtype='datetime64[ns]', freq=None)
# Indices between 10:00 and 12:00 (excluding start): [11 12 35 36 59 60]
# Corresponding dates: DatetimeIndex(['2024-11-14 11:00:00', '2024-11-14 12:00:00',
#                '2024-11-15 11:00:00', '2024-11-15 12:00:00',
#                '2024-11-16 11:00:00', '2024-11-16 12:00:00'],
#               dtype='datetime64[ns]', freq=None)
1024、pandas.DatetimeIndex.normalize方法
1024-1、语法
# 1024、pandas.DatetimeIndex.normalize方法
pandas.DatetimeIndex.normalize(*args, **kwargs)
Convert times to midnight.

The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases, when the time does not matter. Length is unaltered. The timezones are unaffected.

This method is available on Series with datetime values under the .dt accessor, and directly on Datetime Array/Index.

Returns:
DatetimeArray, DatetimeIndex or Series
The same type as the original data. Series will have the same name and index. DatetimeIndex will have the same name.
1024-2、参数

1024-2-1、*args(可选)其他位置参数,为后续扩展功能做预留。

1024-2-2、**kwargs(可选)其他关键字参数,为后续扩展功能做预留。

1024-3、功能

        用于将DatetimeIndex中的所有时间戳调整为相同的日期部分,具体来说就是将时间部分归零,对于比较或对齐时间序列数据非常有用。

1024-4、返回值

        返回一个新的DatetimeIndex,其中所有的时间部分都被设置为00:00:00(即午夜)。

1024-5、说明

        无

1024-6、用法
1024-6-1、数据准备
1024-6-2、代码示例
# 1024、pandas.DatetimeIndex.normalize方法
import pandas as pd
# 创建一个包含多个日期时间的DatetimeIndex
dates = pd.to_datetime(['2024-11-14 10:30:00', '2024-11-15 12:45:00', '2024-11-16 15:00:00'])
datetime_index = pd.DatetimeIndex(dates)
# 归一化DatetimeIndex
normalized_index = datetime_index.normalize()
print("原始DatetimeIndex:")
print(datetime_index)
print("\n归一化后的DatetimeIndex:")
print(normalized_index)
1024-6-3、结果输出
# 1024、pandas.DatetimeIndex.normalize方法
# 原始DatetimeIndex:
# DatetimeIndex(['2024-11-14 10:30:00', '2024-11-15 12:45:00',
#                '2024-11-16 15:00:00'],
#               dtype='datetime64[ns]', freq=None)
# 
# 归一化后的DatetimeIndex:
# DatetimeIndex(['2024-11-14', '2024-11-15', '2024-11-16'], dtype='datetime64[ns]', freq='D')
1025、pandas.DatetimeIndex.strftime方法
1025-1、语法
# 1025、pandas.DatetimeIndex.strftime方法
pandas.DatetimeIndex.strftime(date_format)
Convert to Index using specified date_format.

Return an Index of formatted strings specified by date_format, which supports the same string format as the python standard library. Details of the string format can be found in python string format doc.

Formats supported by the C strftime API but not by the python string format doc (such as “%R”, “%r”) are not officially supported and should be preferably replaced with their supported equivalents (such as “%H:%M”, “%I:%M:%S %p”).

Note that PeriodIndex support additional directives, detailed in Period.strftime.

Parameters:
date_format
str
Date format string (e.g. “%Y-%m-%d”).

Returns:
ndarray[object]
NumPy ndarray of formatted strings.
1025-2、参数

1025-2-1、date_format(必需)一个字符串,表示日期和时间的格式,与Python的strftime方法一致,您可以使用各种格式代码来指定要显示的日期和时间信息。

1025-3、功能

        用于将DatetimeIndex中的日期时间对象格式化为指定的字符串格式,该方法通常用于将时间戳转换为更易读的字符串格式,以便于展示或记录。

1025-4、返回值

        返回一个包含格式化字符串的NumPy数组(numpy.ndarray),每个元素对应于DatetimeIndex中的相应时间戳。

1025-5、说明

        无

1025-6、用法
1025-6-1、数据准备
1025-6-2、代码示例
# 1025、pandas.DatetimeIndex.strftime方法
import pandas as pd
# 创建一个包含多个日期时间的DatetimeIndex
dates = pd.to_datetime(['2024-11-14 10:30:00', '2024-11-15 12:45:00', '2024-11-16 15:00:00'])
datetime_index = pd.DatetimeIndex(dates)
# 使用strftime格式化日期和时间
formatted_dates = datetime_index.strftime('%Y-%m-%d %H:%M:%S')
print("原始DatetimeIndex:")
print(datetime_index)
print("\n格式化后的字符串:")
print(formatted_dates)
1025-6-3、结果输出
# 1025、pandas.DatetimeIndex.strftime方法
# 原始DatetimeIndex:
# DatetimeIndex(['2024-11-14 10:30:00', '2024-11-15 12:45:00',
#                '2024-11-16 15:00:00'],
#               dtype='datetime64[ns]', freq=None)
# 
# 格式化后的字符串:
# Index(['2024-11-14 10:30:00', '2024-11-15 12:45:00', '2024-11-16 15:00:00'], dtype='object')

二、推荐阅读

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2241491.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

从基础到进阶,Dockerfile 如何使用环境变量

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 文章内容 📒📝 什么是 Dockerfile 环境变量?🔖1. `ENV` 指令🔖2. `ARG` 指令🔖语法:🔖使用 `ARG` 的例子:📝 如何使用环境变量提高 Dockerfile 的灵活性🔖1. 动态配置环境🔖2. 配置不同的运行环境🔖3. 多…

2002.6 Partitioning the UMLS semantic network.划分 UMLS 语义网络

Partitioning the UMLS semantic network | IEEE Journals & Magazine | IEEE Xplore 问题 统一医学语言系统(UMLS)语义网络中的语义类型(ST)在知识表示和应用中存在不足,例如 ST 的组织方式缺乏直观性和可解释性…

帽子矩阵--记录

帽子矩阵(Hat Matrix)并不是由某一位具体的科学家单独发明的,而是逐渐在统计学和线性代数的发展过程中形成的。帽子矩阵的概念最早出现在20世纪初的统计学文献中,尤其是在回归分析的研究中得到了广泛应用。然而,具体是…

vue面试题8|[2024-11-14]

问题1:什么是渐进式框架? vue.js router vuex element ...插件 vue.js 渐0 router 渐1 vuex 渐2 vue.js只是一个核心库,比如我再添加一个router或者vuex,不断让项目壮大,就是渐进式框…

web与网络编程

使用HTTP协议访问Web 通过发送请求获取服务器资源的Web浏览器等,被成为客户端(client)。 Web使用一种名为HTTP(超文本传输协议)的协议作为规范,完成从客户端到服务器端等一系列运作流程。 可以说,Web时建立在HTTP协议上通信的。 网络基础T…

docker 部署freeswitch(非编译方式)

一:安装部署 1.拉取镜像 参考:https://hub.docker.com/r/safarov/freeswitch docker pull safarov/freeswitch 2.启动镜像 docker run --nethost --name freeswitch \-e SOUND_RATES8000:16000 \-e SOUND_TYPESmusic:en-us-callie \-v /home/xx/f…

opencv kdtree pcl kdtree 效率对比

由于项目中以一个环节需要使用kdtree ,对性能要求比较严苛&#xff0c;所以看看那个kdtree效率高一些。对比了opencv和pcl。 #include <array> #include <deque> #include <fstream> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp…

ab (Apache Bench)的使用

Apache Bench&#xff08;ab&#xff09;是一个用于基准测试HTTP Web服务器的命令行工具&#xff0c;广泛用于评估和优化Web服务器的性能。以下是关于Apache Bench的详细介绍&#xff0c;包括其功能、使用方法、常用参数和输出结果解析。 功能 性能测试&#xff1a;通过模拟多…

【数据分享】全国农产品成本收益资料汇编(1953-2024)

数据介绍 一、《全国农产品成本收益资料汇编 2024》收录了我国2023年主要农产品生产成本和收益资料及 2018年以来六年的成本收益简明数据。其中全国性数据均未包括香港、澳门特别行政区和台湾省数据。 二、本汇编共分七个部分,即:第一部分,综合;第二部分,各地区粮食、油料;第…

基于OpenCV的图片人脸检测研究

目录 摘要 第一章 引言 第二章 基于 OpenCV 的图片人脸检测 2.1 实现原理 2.2 代码实现与分析 2.3 代码详细分析 第三章 实验结果与分析 第四章 OpenCV 人脸检测的优势与局限性 4.1 优势 4.2 局限性 第五章 结论 第六章 未来展望 参考文献 摘要 人脸检测是计算机视…

BI(Bilinear interpolation)双线性插值实现上采样

在深度学习中 上采样是将图像放大 如上图所示 要求放大后的图像坐标(2,1)处的像素值 要找到目标图像中对应的原图像素 需要与扩大前和扩大后的边长比相乘得到一个坐标(1.5,0.75) 对应原图中没有一个像素点是重合的 蓝色框框的像素值与红色框框的四个点的像素值有关 相关的计算方…

多模态大模型简介

多模态大模型是机器学习领域的一个新兴趋势&#xff0c;它结合了文本、图像、音频等多种数据模态&#xff0c;以实现更全面和深入的信息理解和处理。这种模型能够处理跨模态任务&#xff0c;如图像标注、视觉问答、文本到图像的生成等&#xff0c;是人工智能领域的重要进展。 技…

Python 正则表达式的一些介绍和使用方法说明(数字、字母和数字、电子邮件地址、网址、电话号码(简单)、IPv4 )

## 正则表达式的概念和用途 正则表达式&#xff08;Regular Expression&#xff0c;简称Regex&#xff09;是对字符串操作的一种逻辑公式&#xff0c;由一些事先定义好的特定字符以及这些特定字符的组合所构成。这些特定字符及其组合被用来描述在搜索文本时要匹配的一个或多个…

java排序算法汇总

一、排序算法我介绍 1.1、介绍 排序也称排序算法(Sort Algorithm)&#xff0c;排序是将一组数据&#xff0c;依指定的顺序进行排列的过程。 1.2、排序的分类&#xff1a; 1) 内部排序&#xff1a;指将需要处理的所有数据都加载到内部存储器中进行排序。 2) 外部排序法&…

Ubuntu22.04.2 k8s部署

k8s介绍 简单介绍 通俗易懂的解释&#xff1a; Kubernetes&#xff08;也被称为 K8s&#xff09;就像是一个大管家&#xff0c;帮你管理你的云计算服务。想象一下&#xff0c;你有很多个小程序&#xff08;我们称之为“容器”&#xff09;&#xff0c;每个都在做不同的事情&…

FastGPT部署通义千问Qwen和智谱glm模型|OneAPI配置免费的第三方API

继这篇博客之后 从零开始FastGPT本地部署|Windows 有同学问&#xff0c;不想在多个平台申请API-Key&#xff0c;不好管理且要付费&#xff0c;有木有白嫖方案呀&#xff1f; 答&#xff1a;有啊。用硅基流动。 注册方法看这篇 【1024送福利】硅基流动送2000万token啦&#xff0…

每日OJ题_牛客_DP36 abb_C++_Java

目录 牛客_DP36 abb 题目解析 C代码1暴力 C代码2DP Java代码 牛客_DP36 abb abb_牛客题霸_牛客网 描述&#xff1a; leafee 最近爱上了 abb 型语句&#xff0c;比如“叠词词”、“恶心心” leafee 拿到了一个只含有小写字母的字符串&#xff0c;她想知道有多少个 &quo…

Redis五大基本类型——String字符串命令详解(命令用法详解+思维导图详解)

目录 一、String字符串类型介绍 二、常见命令 1、SET 2、GET 3、MGET 4、MSET 使用MGET 和 使用多次GET的区别 5、DEL 6、SETNX SET、SET NX和SET XX执行流程 7、INCR 8、INCRBY 9、DECR 10、DECYBY 11、INCRBYFLOAT 12、APPEND 13、GETRANGE 14、SETRANGE …

Dubbo 3.x源码(25)—Dubbo服务引用源码(8)notify订阅服务通知更新

基于Dubbo 3.1&#xff0c;详细介绍了Dubbo服务的发布与引用的源码。 此前我们学习了接口级的服务引入订阅的refreshInterfaceInvoker方法&#xff0c;当时还有最为关键的notify服务通知更新的部分源码没有学习&#xff0c;本次我们来学习notify通知本地服务更新的源码。 Dubb…

STM32+AI语音识别智能家居系统

基于 STM32 和 AI 语音识别的智能家居系统的详细硬件和软件设计&#xff0c;包括各个模块的详细描述和代码示例。 一、硬件设计 1. 微控制器&#xff08;STM32&#xff09;&#xff1a; 选择 STM32F7 系列或更高性能的芯片&#xff0c;如 STM32F767ZIT6&#xff0c;以满足处理…