目錄
一、用法精講
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.Warningdayfirst=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).Warningyearfirst=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.WarningIn 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.NoteIf 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]