janitor.change_type¶
-
janitor.
change_type
(df: pandas.core.frame.DataFrame, column_name: Hashable, dtype: type, ignore_exception: bool = False) → pandas.core.frame.DataFrame[source]¶ Change the type of a column.
This method mutates the original DataFrame.
Exceptions that are raised can be ignored. For example, if one has a mixed dtype column that has non-integer strings and integers, and you want to coerce everything to integers, you can optionally ignore the non-integer strings and replace them with
NaN
or keep the original valueIntended to be the method-chaining alternative to:
df[col] = df[col].astype(dtype)
Method chaining syntax:
df = pd.DataFrame(...).change_type('col1', str)
- Parameters
df – A pandas dataframe.
column_name – A column in the dataframe.
dtype – The datatype to convert to. Should be one of the standard Python types, or a numpy datatype.
ignore_exception – one of
{False, "fillna", "keep_values"}
.
- Returns
A pandas DataFrame with changed column types.
- Raises
ValueError – if unknown option provided for
ignore_exception
.