当前位置:   article > 正文

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

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

目录

一、用法精讲

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、语法
  1. # 61、pandas.to_numeric函数
  2. pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=_NoDefault.no_default)
  3. Convert argument to a numeric type.
  4. The default return dtype is float64 or int64 depending on the data supplied. Use the downcast parameter to obtain other dtypes.
  5. 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.
  6. Parameters:
  7. argscalar, list, tuple, 1-d array, or Series
  8. Argument to be converted.
  9. errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise
  10. If ‘raise’, then invalid parsing will raise an exception.
  11. If ‘coerce’, then invalid parsing will be set as NaN.
  12. If ‘ignore’, then invalid parsing will return the input.
  13. Changed in version 2.2.
  14. “ignore” is deprecated. Catch exceptions explicitly instead.
  15. downcaststr, default None
  16. Can be ‘integer’, ‘signed’, ‘unsigned’, orfloat’. 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:
  17. ‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8)
  18. ‘unsigned’: smallest unsigned int dtype (min.: np.uint8)
  19. float’: smallest float dtype (min.: np.float32)
  20. 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.
  21. 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.
  22. dtype_backend{‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’
  23. Back-end data type applied to the resultant DataFrame (still experimental). Behaviour is as follows:
  24. "numpy_nullable": returns nullable-dtype-backed DataFrame (default).
  25. "pyarrow": returns pyarrow-backed nullable ArrowDtype DataFrame.
  26. New in version 2.0.
  27. Returns:
  28. ret
  29. 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、代码示例
  1. # 61、pandas.to_numeric函数
  2. # 61-1、转换Series
  3. import pandas as pd
  4. data = pd.Series(['1', '2', '3', 'apple', '5'])
  5. # 转换为数字,遇到错误将其转换为NaN
  6. numeric_data = pd.to_numeric(data, errors='coerce')
  7. print(numeric_data, end='\n\n')
  8. # 61-2、转换DataFrame
  9. import pandas as pd
  10. df = pd.DataFrame({
  11. 'A': ['1', '2', '3', 'apple', '5'],
  12. 'B': ['10.5', '20.1', '30.2', '40.0', '50.5']
  13. })
  14. # 转换为数字,遇到错误将其转换为NaN
  15. numeric_df = df.apply(pd.to_numeric, errors='coerce')
  16. print(numeric_df)
61-6-3、结果输出
  1. # 61、pandas.to_numeric函数
  2. # 61-1、转换Series
  3. # 0 1.0
  4. # 1 2.0
  5. # 2 3.0
  6. # 3 NaN
  7. # 4 5.0
  8. # dtype: float64
  9. # 61-2、转换DataFrame
  10. # A B
  11. # 0 1.0 10.5
  12. # 1 2.0 20.1
  13. # 2 3.0 30.2
  14. # 3 NaN 40.0
  15. # 4 5.0 50.5
62、pandas.to_datetime函数
62-1、语法
  1. # 62、pandas.to_datetime函数
  2. 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)
  3. Convert argument to datetime.
  4. This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object.
  5. Parameters:
  6. argint, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like
  7. 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.
  8. errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise
  9. If 'raise', then invalid parsing will raise an exception.
  10. If 'coerce', then invalid parsing will be set as NaT.
  11. If 'ignore', then invalid parsing will return the input.
  12. dayfirstbool, default False
  13. 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.
  14. Warning
  15. dayfirst=True is not strict, but will prefer to parse with day first.
  16. yearfirstbool, default False
  17. Specify a date parse order if arg is str or is list-like.
  18. If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
  19. If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
  20. Warning
  21. yearfirst=True is not strict, but will prefer to parse with year first.
  22. utcbool, default False
  23. Control timezone-related parsing, localization and conversion.
  24. 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.
  25. 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.
  26. Warning
  27. 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.
  28. See also: pandas general documentation about timezone conversion and localization.
  29. formatstr, default None
  30. 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:
  31. “ISO8601”, to parse any ISO8601 time string (not necessarily in exactly the same format);
  32. “mixed”, to infer the format for each element individually. This is risky, and you should probably use it along with dayfirst.
  33. Note
  34. If a DataFrame is passed, then format has no effect.
  35. exactbool, default True
  36. Control how format is used:
  37. If True, require an exact format match.
  38. If False, allow the format to match anywhere in the target string.
  39. Cannot be used alongside format='ISO8601' or format='mixed'.
  40. unitstr, default ‘ns’
  41. 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.
  42. infer_datetime_formatbool, default False
  43. 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.
  44. Deprecated since version 2.0.0: A strict version of this argument is now the default, passing it has no effect.
  45. originscalar, default ‘unix’
  46. Define the reference date. The numeric values would be parsed as number of units (defined by unit) since this reference date.
  47. If 'unix' (or POSIX) time; origin is set to 1970-01-01.
  48. 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.
  49. If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date string), origin is set to Timestamp identified by origin.
  50. If a float or integer, origin is the difference (in units determined by the unit argument) relative to 1970-01-01.
  51. cachebool, default True
  52. 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.
  53. Returns:
  54. datetime
  55. 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):
  56. scalar: Timestamp (or datetime.datetime)
  57. array-like: DatetimeIndex (or Series with object dtype containing datetime.datetime)
  58. Series: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime)
  59. DataFrame: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime)
  60. Raises:
  61. ParserError
  62. When parsing a date from string fails.
  63. ValueError
  64. 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、代码示例
  1. # 62、pandas.to_datetime函数
  2. # 62-1、将字符串转换为datetime
  3. import pandas as pd
  4. date_str = '2024-07-15'
  5. date = pd.to_datetime(date_str)
  6. print(date, end='\n\n')
  7. # 62-2、将列表转换为datetime
  8. import pandas as pd
  9. date_list = ['2024-07-15', '2025-07-15']
  10. dates = pd.to_datetime(date_list)
  11. print(dates, end='\n\n')
  12. # 62-3、处理Series并处理错误
  13. import pandas as pd
  14. date_series = pd.Series(['2024-07-15', '2025-07-15', 'not a date'])
  15. dates = pd.to_datetime(date_series, errors='coerce')
  16. print(dates, end='\n\n')
  17. # 62-4、使用特定格式解析日期
  18. import pandas as pd
  19. date_str = '15/07/2024'
  20. date = pd.to_datetime(date_str, format='%d/%m/%Y', dayfirst=True)
  21. print(date, end='\n\n')
  22. # 62-5、将时间戳转换为datetime
  23. import pandas as pd
  24. timestamp_series = pd.Series([1626357600, 1626358200])
  25. dates = pd.to_datetime(timestamp_series, unit='s')
  26. print(dates)
62-6-3、结果输出 
  1. # 62、pandas.to_datetime函数
  2. # 62-1、将字符串转换为datetime
  3. # 2024-07-15 00:00:00
  4. # 62-2、将列表转换为datetime
  5. # DatetimeIndex(['2024-07-15', '2025-07-15'], dtype='datetime64[ns]', freq=None)
  6. # 62-3、处理Series并处理错误
  7. # 0 2024-07-15
  8. # 1 2025-07-15
  9. # 2 NaT
  10. # dtype: datetime64[ns]
  11. # 62-4、使用特定格式解析日期
  12. # 2024-07-15 00:00:00
  13. # 62-5、将时间戳转换为datetime
  14. # 0 2021-07-15 14:00:00
  15. # 1 2021-07-15 14:10:00
  16. # dtype: datetime64[ns]

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/890501
推荐阅读
相关标签
  

闽ICP备14008679号