类型转换 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 @ 2024-09-20 18:33  papering  阅读(52)  评论(0编辑  收藏  举报