pandas.Series.infer_objects#
- Series.infer_objects(copy=None)[源代码]#
尝试为对象列推断更好的数据类型(dtypes)。
尝试对 object 类型的数据列进行软转换,将非 object 类型和无法转换的列保持不变。推断规则与正常 Series/DataFrame 构建期间的规则相同。
- Parameters:
- copybool, default True
是否为非 object 类型或不可推断的列或 Series 创建副本。
备注
copy 关键字在 pandas 3.0 中将更改行为。Copy-on-Write 将默认启用,这意味着所有带有 copy 关键字的方法都将使用惰性复制机制来延迟复制并忽略 copy 关键字。copy 关键字将在 pandas 的未来版本中移除。
通过启用 copy on write
pd.options.mode.copy_on_write = True,您可以获得未来的行为和改进。
- Returns:
- 与输入对象相同的类型
参见
to_datetime将参数转换为 datetime。
to_timedelta将参数转换为 timedelta。
to_numeric将参数转换为数值类型。
convert_dtypes将参数转换为最佳可用 dtype。
Examples
>>> df = pd.DataFrame({"A": ["a", 1, 2, 3]}) >>> df = df.iloc[1:] >>> df A 1 1 2 2 3 3
>>> df.dtypes A object dtype: object
>>> df.infer_objects().dtypes A int64 dtype: object