modin.pandas.DataFrame.astype¶
- DataFrame.astype(dtype: str | type | pd.Series | dict[str, type], copy: bool = True, errors: Literal['raise', 'ignore'] = 'raise') pd.DataFrame | pd.Series [source]¶
Cast a pandas object to a specified dtype
dtype
.- Parameters:
dtype (str, 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.
copy (bool, default True) – Return a copy (i.e., a new object) when
copy=True
; otherwise, astype operates inplace (be very careful settingcopy=False
as changes to values then may propagate to other pandas objects).errors ({'raise', 'ignore'}, default 'raise') –
Control raising of exceptions on invalid data for provided dtype.
raise
: allow exceptions to be raisedignore
: suppress exceptions. On error return original object.
- Return type:
same type as caller (Snowpark pandas
DataFrame
or Snowpark pandasSeries
)
Examples
Create a DataFrame:
>>> d = {'col1': [1, 2], 'col2': [3, 4]} >>> df = pd.DataFrame(data=d) >>> df.dtypes col1 int64 col2 int64 dtype: object
Cast all columns to int32 (dtypes will be int64 since Snowpark pandas API will cast all integers to int64):
>>> df.astype('int32').dtypes col1 int64 col2 int64 dtype: object
Cast col1 to float64 using a dictionary:
>>> df.astype({'col1': 'float64'}).dtypes col1 float64 col2 int64 dtype: object
Create a series:
>>> ser = pd.Series([1, 2], dtype=str) >>> ser 0 1 1 2 dtype: object >>> ser.astype('float64') 0 1.0 1 2.0 dtype: float64