类型转换 Cast a pandas object to a specified dtype ``dtype``.

实践:

修改列值

分组、排序使用同一字段:整数

--》区间名称 字符串

 

 

                FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[320, 439)' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
                  Cast a pandas object to a specified dtype ``dtype``.
 
 
 

 

pandas.DataFrame.astype — pandas 2.2.2 documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.astype.html

pandas.DataFrame.astype

DataFrame.astype(dtypecopy=Noneerrors='raise')[source]

Cast a pandas object to a specified dtype dtype.

Parameters:
dtypestr, data type, Series or Mapping of column name -> data type

Use a str, numpy.dtype, pandas.ExtensionDtype or Python type to cast entire pandas object to the same type. Alternatively, use a mapping, e.g. {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types.

copybool, default True

Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).

Note

The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.

You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write True

errors{‘raise’, ‘ignore’}, default ‘raise’

Control raising of exceptions on invalid data for provided dtype.

  • raise : allow exceptions to be raised

  • ignore : suppress exceptions. On error return original object.

Returns:
same type as caller

See also

to_datetime

Convert argument to datetime.

to_timedelta

Convert argument to timedelta.

to_numeric

Convert argument to a numeric type.

numpy.ndarray.astype

Cast a numpy array to a specified type.

 

 

 

 

posted @   papering  阅读(159)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2023-09-20 flink 结果表为mysql null、浮点入表
2023-09-20 css关于/deep/的解释和用法
2022-09-20 中国汽车基础软件发展白皮书3.0
2019-09-20 降低复杂度的一个原则就是尽可能减少需要处理异常的可能性。
2019-09-20 The difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data
2019-09-20 EnvironmentError: mysql_config not found
2019-09-20 线上一次大量 CLOSE_WAIT 复盘
点击右上角即可分享
微信分享提示