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

news2024/9/24 5:22:24

目录

一、用法精讲

61、pandas.to_numeric函数

61-1、语法

61-2、参数

61-3、功能

61-4、返回值

61-5、说明

61-6、用法

61-6-1、数据准备

61-6-2、代码示例

61-6-3、结果输出

62、pandas.to_datetime函数

62-1、语法

62-2、参数

62-3、功能

62-4、返回值

62-5、说明

62-6、用法

62-6-1、数据准备

62-6-2、代码示例

62-6-3、结果输出 

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

61、pandas.to_numeric函数
61-1、语法
# 61、pandas.to_numeric函数
pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=_NoDefault.no_default)
Convert argument to a numeric type.

The default return dtype is float64 or int64 depending on the data supplied. Use the downcast parameter to obtain other dtypes.

Please note that precision loss may occur if really large numbers are passed in. Due to the internal limitations of ndarray, if numbers smaller than -9223372036854775808 (np.iinfo(np.int64).min) or larger than 18446744073709551615 (np.iinfo(np.uint64).max) are passed in, it is very likely they will be converted to float so that they can be stored in an ndarray. These warnings apply similarly to Series since it internally leverages ndarray.

Parameters:
argscalar, list, tuple, 1-d array, or Series
Argument to be converted.

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
If ‘raise’, then invalid parsing will raise an exception.

If ‘coerce’, then invalid parsing will be set as NaN.

If ‘ignore’, then invalid parsing will return the input.

Changed in version 2.2.

“ignore” is deprecated. Catch exceptions explicitly instead.

downcaststr, default None
Can be ‘integer’, ‘signed’, ‘unsigned’, or ‘float’. If not None, and if the data has been successfully cast to a numerical dtype (or if the data was numeric to begin with), downcast that resulting data to the smallest numerical dtype possible according to the following rules:

‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8)

‘unsigned’: smallest unsigned int dtype (min.: np.uint8)

‘float’: smallest float dtype (min.: np.float32)

As this behaviour is separate from the core conversion to numeric values, any errors raised during the downcasting will be surfaced regardless of the value of the ‘errors’ input.

In addition, downcasting will only occur if the size of the resulting data’s dtype is strictly larger than the dtype it is to be cast to, so if none of the dtypes checked satisfy that specification, no downcasting will be performed on the data.

dtype_backend{‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’
Back-end data type applied to the resultant DataFrame (still experimental). Behaviour is as follows:

"numpy_nullable": returns nullable-dtype-backed DataFrame (default).

"pyarrow": returns pyarrow-backed nullable ArrowDtype DataFrame.

New in version 2.0.

Returns:
ret
Numeric if parsing succeeded. Return type depends on input. Series if Series, otherwise ndarray.
61-2、参数

61-2-1、arg(必须)表示你想要转换的数据,可以是一个单独的数值、列表、Series或者DataFrame。

61-2-2、errors(可选,默认值为'raise')指定在遇到不能转换为数字的值时的处理方式,可选的值有:

61-2-2-1、'raise'(默认值):遇到错误时会引发异常。

61-2-2-2、'coerce':遇到不能转换为数字的值时,将其转换为NaN(缺失值)。

61-2-2-3、'ignore':忽略不能转换为数字的值,保持原样。

61-2-3、downcast(可选,默认值为None)用于将数据转换为较低精度的数值类型,以减少内存使用,可选值有:

61-2-3-1、None(默认值):不进行降级。

61-2-3-2、'integer':尽可能转换为较小的整数类型。

61-2-3-3、'signed':尽可能转换为较小的有符号整数类型。

61-2-3-4、'unsigned':尽可能转换为较小的无符号整数类型。

61-2-3-5、'float':尽可能转换为较小的浮点数类型。

61-2-4、dtype_backend(可选)内部调用,一般不需要用户直接设置。

61-3、功能

        用于将参数(如单个值、列表、Series或者DataFrame)中的数据转换为数字类型(整数或浮点数)。

61-4、返回值

        函数的返回值取决于输入数据的类型:

61-4-1、单个值:如果输入是单个值,返回一个转换后的数值(整数或浮点数)。

61-4-2、列表:如果输入是列表,返回一个包含转换后数值的列表。

61-4-3、Series:如果输入是pandas Series,返回一个转换后的pandas Series,类型为数值类型。

61-4-4、DataFrame:如果输入是pandas DataFrame,返回一个转换后的DataFrame,每一列都会尝试转换为数值类型。

61-5、说明

        该函数通过灵活的参数设置,能够有效地将不同类型的数据转换为数值类型,并提供多种错误处理选项,适用于数据预处理和清洗的各类场景。

61-6、用法
61-6-1、数据准备
61-6-2、代码示例
# 61、pandas.to_numeric函数
# 61-1、转换Series
import pandas as pd
data = pd.Series(['1', '2', '3', 'apple', '5'])
# 转换为数字,遇到错误将其转换为NaN
numeric_data = pd.to_numeric(data, errors='coerce')
print(numeric_data, end='\n\n')

# 61-2、转换DataFrame
import pandas as pd
df = pd.DataFrame({
    'A': ['1', '2', '3', 'apple', '5'],
    'B': ['10.5', '20.1', '30.2', '40.0', '50.5']
})
# 转换为数字,遇到错误将其转换为NaN
numeric_df = df.apply(pd.to_numeric, errors='coerce')
print(numeric_df)
61-6-3、结果输出
# 61、pandas.to_numeric函数
# 61-1、转换Series
# 0    1.0
# 1    2.0
# 2    3.0
# 3    NaN
# 4    5.0
# dtype: float64

# 61-2、转换DataFrame
#      A     B
# 0  1.0  10.5
# 1  2.0  20.1
# 2  3.0  30.2
# 3  NaN  40.0
# 4  5.0  50.5
62、pandas.to_datetime函数
62-1、语法
# 62、pandas.to_datetime函数
pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=False, format=None, exact=_NoDefault.no_default, unit=None, infer_datetime_format=_NoDefault.no_default, origin='unix', cache=True)
Convert argument to datetime.

This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object.

Parameters:
argint, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like
The object to convert to a datetime. If a DataFrame is provided, the method expects minimally the following columns: "year", "month", "day". The column “year” must be specified in 4-digit format.

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
If 'raise', then invalid parsing will raise an exception.

If 'coerce', then invalid parsing will be set as NaT.

If 'ignore', then invalid parsing will return the input.

dayfirstbool, default False
Specify a date parse order if arg is str or is list-like. If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.

Warning

dayfirst=True is not strict, but will prefer to parse with day first.

yearfirstbool, default False
Specify a date parse order if arg is str or is list-like.

If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.

If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).

Warning

yearfirst=True is not strict, but will prefer to parse with year first.

utcbool, default False
Control timezone-related parsing, localization and conversion.

If True, the function always returns a timezone-aware UTC-localized Timestamp, Series or DatetimeIndex. To do this, timezone-naive inputs are localized as UTC, while timezone-aware inputs are converted to UTC.

If False (default), inputs will not be coerced to UTC. Timezone-naive inputs will remain naive, while timezone-aware ones will keep their time offsets. Limitations exist for mixed offsets (typically, daylight savings), see Examples section for details.

Warning

In a future version of pandas, parsing datetimes with mixed time zones will raise an error unless utc=True. Please specify utc=True to opt in to the new behaviour and silence this warning. To create a Series with mixed offsets and object dtype, please use apply and datetime.datetime.strptime.

See also: pandas general documentation about timezone conversion and localization.

formatstr, default None
The strftime to parse time, e.g. "%d/%m/%Y". See strftime documentation for more information on choices, though note that "%f" will parse all the way up to nanoseconds. You can also pass:

“ISO8601”, to parse any ISO8601 time string (not necessarily in exactly the same format);

“mixed”, to infer the format for each element individually. This is risky, and you should probably use it along with dayfirst.

Note

If a DataFrame is passed, then format has no effect.

exactbool, default True
Control how format is used:

If True, require an exact format match.

If False, allow the format to match anywhere in the target string.

Cannot be used alongside format='ISO8601' or format='mixed'.

unitstr, default ‘ns’
The unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix', this would calculate the number of milliseconds to the unix epoch start.

infer_datetime_formatbool, default False
If True and no format is given, attempt to infer the format of the datetime strings based on the first non-NaN element, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x.

Deprecated since version 2.0.0: A strict version of this argument is now the default, passing it has no effect.

originscalar, default ‘unix’
Define the reference date. The numeric values would be parsed as number of units (defined by unit) since this reference date.

If 'unix' (or POSIX) time; origin is set to 1970-01-01.

If 'julian', unit must be 'D', and origin is set to beginning of Julian Calendar. Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC.

If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date string), origin is set to Timestamp identified by origin.

If a float or integer, origin is the difference (in units determined by the unit argument) relative to 1970-01-01.

cachebool, default True
If True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. The cache is only used when there are at least 50 values. The presence of out-of-bounds values will render the cache unusable and may slow down parsing.

Returns:
datetime
If parsing succeeded. Return type depends on input (types in parenthesis correspond to fallback in case of unsuccessful timezone or out-of-range timestamp parsing):

scalar: Timestamp (or datetime.datetime)

array-like: DatetimeIndex (or Series with object dtype containing datetime.datetime)

Series: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime)

DataFrame: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime)

Raises:
ParserError
When parsing a date from string fails.

ValueError
When another datetime conversion error happens. For example when one of ‘year’, ‘month’, day’ columns is missing in a DataFrame, or when a Timezone-aware datetime.datetime is found in an array-like of mixed time offsets, and utc=False.
62-2、参数

62-2-1、arg(必须)表示需要转换为日期时间的对象,可以是单个日期时间字符串、日期时间对象、列表、Series或DataFrame。

62-2-2、errors(可选,默认值为'raise')指定在遇到不能转换为数字的值时的处理方式,可选的值有:

62-2-2-1、'raise'(默认值):遇到错误时会引发异常。

62-2-2-2、'coerce':遇到不能转换为数字的值时,将其转换为NaN(缺失值)。

62-2-2-3、'ignore':忽略不能转换为数字的值,保持原样。

62-2-3、dayfirst(可选,默认值为False)当为True时,解析日期时优先将前两位作为日,例如:dayfirst=True将'10/11/2024'解析为2024年11月10日。

62-2-4、yearfirst(可选,默认值为False)当为True时,解析日期时优先将前两位作为年,例如:yearfirst=True将'2024-10-11'解析为2024年10月11日。

62-2-5、utc(可选,默认值为False)当为True时,将时间转换为UTC时间。

62-2-6、format(可选,默认值为None)指定日期时间字符串的格式,例如:format='%Y-%m-%d %H:%M:%S'。

62-2-7、exact(可选)当为True时,要求日期时间字符串完全匹配格式。

62-2-8、unit(可选,默认值为None)如果传入的是整数或浮点数,指定其时间单位,如s(秒),ms(毫秒),us(微秒),ns(纳秒)。

62-2-9、infer_datetime_format(可选)当为True时,自动推断日期时间字符串的格式以提高解析速度。

62-2-10、origin(可选,默认值为'unix')指定时间计算的起点,可以是'unix'(1970-01-01),也可以是具体的时间字符串。

62-2-11、cache(可选,默认值为True)当为True时,启用缓存以提高解析速度。

62-3、功能

        用于将各种格式的输入数据转换为datetime64[ns]类型,确保数据在后续分析中具有一致的日期时间格式。

62-4、返回值

        返回值类型取决于输入:

62-4-1、如果输入是单个字符串或单个数值,则返回一个Timestamp对象。

62-4-2、如果输入是列表、数组、Series或DataFrame,则返回一个DatetimeIndex或Series,其中包含转换后的日期时间数据。

62-5、说明

        无

62-6、用法
62-6-1、数据准备
62-6-2、代码示例
# 62、pandas.to_datetime函数
# 62-1、将字符串转换为datetime
import pandas as pd
date_str = '2024-07-15'
date = pd.to_datetime(date_str)
print(date, end='\n\n')

# 62-2、将列表转换为datetime
import pandas as pd
date_list = ['2024-07-15', '2025-07-15']
dates = pd.to_datetime(date_list)
print(dates, end='\n\n')

# 62-3、处理Series并处理错误
import pandas as pd
date_series = pd.Series(['2024-07-15', '2025-07-15', 'not a date'])
dates = pd.to_datetime(date_series, errors='coerce')
print(dates, end='\n\n')

# 62-4、使用特定格式解析日期
import pandas as pd
date_str = '15/07/2024'
date = pd.to_datetime(date_str, format='%d/%m/%Y', dayfirst=True)
print(date, end='\n\n')

# 62-5、将时间戳转换为datetime
import pandas as pd
timestamp_series = pd.Series([1626357600, 1626358200])
dates = pd.to_datetime(timestamp_series, unit='s')
print(dates)
62-6-3、结果输出 
# 62、pandas.to_datetime函数
# 62-1、将字符串转换为datetime
# 2024-07-15 00:00:00

# 62-2、将列表转换为datetime
# DatetimeIndex(['2024-07-15', '2025-07-15'], dtype='datetime64[ns]', freq=None)

# 62-3、处理Series并处理错误
# 0   2024-07-15
# 1   2025-07-15
# 2          NaT
# dtype: datetime64[ns]

# 62-4、使用特定格式解析日期
# 2024-07-15 00:00:00

# 62-5、将时间戳转换为datetime
# 0   2021-07-15 14:00:00
# 1   2021-07-15 14:10:00
# dtype: datetime64[ns]

二、推荐阅读

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

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

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

相关文章

为ppt中的文字配色

文字的颜色来源于ppt不可删去的图像的颜色 从各类搜索网站中搜索ppt如何配色,有如下几点: 1.可以使用对比色,表示强调。 2.可以使用近似色,使得和谐统一。 3.最好一张ppt中,使用的颜色不超过三种主要颜色。 但我想强调…

hot100 | 十四、贪心

1-leetcode121. 买卖股票的最佳时机 注意&#xff1a; Labuladong的套路太厉害了&#xff0c;分析的很清晰状态转移方程 public int maxProfit(int[] prices) {int n prices.length;int[][] dp new int[n][2];for (int i 0; i < n; i) {if (i-1 -1){// base casedp[…

【C语言】结构体,枚举,联合超详解!!!

目录 结构体 结构体声明 结构体成员的访问 结构体自引用 结构体变量定义&#xff0c;初始化&#xff0c;传参 结构体内存对齐 位段 枚举 联合(共用体) 结构体 结构体声明 1. 概念 1. 结构体是一些值的集合&#xff0c;这些值称为成员变量。 2. 结构体的每个成员可…

基于SpringBoot+Vue的广场舞团系统(带1w+文档)

基于SpringBootVue的广场舞团系统(带1w文档) 基于SpringBootVue的广场舞团系统(带1w文档) 广场舞团&#xff0c;为用户随时随地查看广场舞团信息提供了便捷的方法&#xff0c;更重要的是大大的简化了管理员管理广场舞团信息的方式方法&#xff0c;更提供了其他想要了解广场舞团…

Java强软弱虚引用的特点以及应用场景(面试重点)

强&#xff1a;即使OOM也不回收软&#xff1a;内存溢出前回收弱&#xff1a;只要垃圾收集就死虚&#xff1a;对垃圾收集没关系&#xff0c;只有得到通知&#xff08;插眼&#xff0c;也操作不了对象、只能看到它还活着&#xff09; 一、软引用 代码示例&#xff1a; public cl…

快手开源LivePortrait,实现表情姿态极速迁移,GitHub 6.5K Star

近日&#xff0c;快手可灵大模型团队开源了名为LivePortrait的可控人像视频生成框架&#xff0c;能够准确、实时地将驱动视频的表情、姿态迁移到静态或动态人像视频上&#xff0c;生成极具表现力的视频结果。如下动图所示&#xff1a; 来自网友测试LivePortrait 来自网友测试Li…

【Linux】Linux进程揭秘:从理论到实践的深度探索之旅

目录 前言&#xff1a;操作系统简介 概念 设计目的 理解 进程&#xff1a;程序的执行之魂 进程和程序的联系与区别 描述进程-PCB 进程的标识符 进程状态 状态转换 僵尸进程 孤儿进程 前言&#xff1a;操作系统简介 概念 操作系统&#xff08;英语&#xff1a;Opera…

PyTorch高级特性与性能优化

PyTorch高级特性与性能优化 引言&#xff1a; 在深度学习项目中&#xff0c;使用正确的工具和优化策略对于实现高效和有效的模型训练至关重要。PyTorch&#xff0c;作为一个流行的深度学习框架&#xff0c;提供了一系列的高级特性和性能优化方法&#xff0c;以帮助开发者充分利…

C#实现数据采集系统-ModbusTCP查询报文分析和实现、通信实现、测试项目

ModbusTcp的应用 Modbus是工业通信协议中广泛使用的协议,大部分设备都支持。Modbus TCP是一种基于TCP/IP网络的工业通信协议,它是Modbus协议的一种变种,专门设计用于在网络上传输数据。 Modbus TCP/IP保留了Modbus串行协议的数据结构和功能特性,同时利用了TCP/IP网络的高…

​污水处理厂空气质量监测——破解恶臭难题的科技钥匙

​ ​引言 ​ ​在城市化进程中&#xff0c;污水处理厂作为净化生活与工业废水的关键设施&#xff0c;扮演着至关重要的角色。然而&#xff0c;随着处理规模的不断扩大&#xff0c;污水处理厂的空气质量问题&#xff0c;尤其是恶臭问题&#xff0c;逐渐成为困扰周边居民和…

spark 事件总线listenerBus

事件总线基本流程 图片来源&#xff1a;https://blog.csdn.net/sinat_26781639/article/details/105012302 LiveListenerBus创建 在sparkContext初始化中创建LiveListenerBus对象。 主要变量有两个 queues&#xff1a;事件队列&#xff0c;里面存放四个队列&#xff0c;每…

python gradio 的输出展示组件

HTML&#xff1a;展示HTML内容&#xff0c;适用于富文本或网页布局。JSON&#xff1a;以JSON格式展示数据&#xff0c;便于查看结构化数据。KeyValues&#xff1a;以键值对形式展示数据。Label&#xff1a;展示文本标签&#xff0c;适用于简单的文本输出。Markdown&#xff1a;…

独立游戏《星尘异变》UE5 C++程序开发日志6——实现存档和基础设置系统

目录 一、存档类 1.创建一个SaveGame类 2.存储关卡内数据 3.加载关卡数据 4.关于定时器 5.存储全局数据 6.加载全局数据 二、存档栏 1.存档栏的数据结构 2.创建新存档 3.覆盖已有存档 4.删除存档 三、游戏的基础设置 1.存储游戏设置的数据结构 2.初始化设置 3.…

JavaScript基础 第四弹 学习笔记

函数 1、为什么需要函数&#xff1f;可以实现代码复用&#xff0c;提高开发效率。 函数的定义 &#xff1a;函数function&#xff0c;是被设计为执行特定任务的代码块。 函数可以把具有相同或相似逻辑的代码‘包裹’起来&#xff0c;通过函数调用执行这些被“包裹”的代码逻…

羊大师:羊奶精华,补钙免疫,养颜促消化

在浩瀚的自然馈赠中&#xff0c;羊奶以其独特的精华&#xff0c;成为了现代人追求健康生活的优选。它不仅仅是一种饮品&#xff0c;更是大自然赋予我们的宝贵滋养圣品。 补钙免疫&#xff0c;守护健康基石 羊奶中富含的钙质&#xff0c;是构建强健骨骼的基石。其高吸收率的特性…

免杀笔记 ----> 动态调用

前一段时间不是说要进行IAT表的隐藏吗&#xff0c;终于给我逮到时间来写了&#xff0c;今天就来先将最简单的一种方式 ----> 动态调用&#xff01;&#xff01;&#xff01; 1.静态查杀 这里还是说一下我们为什么要对他进行隐藏呢&#xff1f;&#xff1f;&#xff1…

HBuilderX打包流程(H5)?HBuilder如何发布前端H5应用?前端开发怎样打包发布uniapp项目为h5?

打包步骤&#xff1a; 1、打开hbuilder x》发行》网站-PC Web或手机H5(仅适用于uni-app)(H) 2、面板里的所有信息都可以不填&#xff0c;也不用勾选》直接点击【发行】即可 3、打包成功&#xff1a; 4、部署 按照打包后的路径&#xff0c;找到打包好的文件夹&#xff0c;把文…

【前端数据层高可用架构】

前端数据层高可用架构 前后端架构模式如下图 在这个架构下,客端数据可用率计算方式: 因此整体数据可用性分析表如下: 只有在客端和 BFF 都正常的情况下数据才能可用,而这种情况占比不是很高,因此整体的用户体验就不是很好。 本次建设目标 本文的设计方案就是要解决…

【前端】表单密码格式—校验。

如图&#xff1a;实现表单输入密码和确认密码的时候进行表单校验。 实现方式&#xff1a; 1.在代码的data里面定义&#xff0c;函数验证的方法。如图所示,代码如下 【代码】如下&#xff1a; const validatePassword (rule, value, callback) > {if (value ) {callback(n…

Java SpringBoot 若依 后端实现评论“盖楼“,“楼中楼“功能 递归查询递归组装评论结构

效果图 数据库设计 还可以使用路径模块 一级评论id,二级评论id, 用like最左匹配原则查询子评论 因为接手遗留代码&#xff0c;需要添加字段&#xff0c;改动数据库&#xff0c;我就不改动了&#xff0c;导致我下面递归查询子评论不是很好。 业务代码 Overridepublic List<S…