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

CSDN 2024-07-18 15:35:03 阅读 98

目录

一、用法精讲

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、语法

<code># 61、pandas.to_numeric函数

pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=_NoDefault.no_default)code>

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')code>

print(numeric_data, end='\n\n')code>

# 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')code>

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)code>

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'.code>

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.code>

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、数据准备

<code>无

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')code>

# 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')code>

# 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')code>

print(dates, end='\n\n')code>

# 62-4、使用特定格式解析日期

import pandas as pd

date_str = '15/07/2024'

date = pd.to_datetime(date_str, format='%d/%m/%Y', dayfirst=True)code>

print(date, end='\n\n')code>

# 62-5、将时间戳转换为datetime

import pandas as pd

timestamp_series = pd.Series([1626357600, 1626358200])

dates = pd.to_datetime(timestamp_series, unit='s')code>

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)code>

# 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、博客个人主页


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。