5
5
import pandas as pd
6
6
import pandas_flavor as pf
7
7
8
- from janitor .utils import deprecated_alias
8
+ from janitor .utils import deprecated_alias , refactored_function
9
9
10
10
11
11
@pf .register_dataframe_method
12
+ @refactored_function (
13
+ message = (
14
+ "This function will be deprecated in a 1.x release. "
15
+ "Please use `pd.DataFrame.astype` instead."
16
+ )
17
+ )
12
18
@deprecated_alias (column = "column_name" )
13
19
def change_type (
14
20
df : pd .DataFrame ,
@@ -18,60 +24,65 @@ def change_type(
18
24
) -> pd .DataFrame :
19
25
"""Change the type of a column.
20
26
21
- This method does not mutate the original DataFrame.
27
+ This method does not mutate the original DataFrame.
22
28
23
- Exceptions that are raised can be ignored. For example, if one has a mixed
24
- dtype column that has non-integer strings and integers, and you want to
25
- coerce everything to integers, you can optionally ignore the non-integer
26
- strings and replace them with `NaN` or keep the original value.
29
+ Exceptions that are raised can be ignored. For example, if one has a mixed
30
+ dtype column that has non-integer strings and integers, and you want to
31
+ coerce everything to integers, you can optionally ignore the non-integer
32
+ strings and replace them with `NaN` or keep the original value.
27
33
28
- Intended to be the method-chaining alternative to:
34
+ Intended to be the method-chaining alternative to:
29
35
30
- ```python
31
- df[col] = df[col].astype(dtype)
36
+ ```python
37
+ df[col] = df[col].astype(dtype)
32
38
```
33
39
34
- Example: Change the type of a column.
40
+ !!!note
41
+
42
+ This function will be deprecated in a 1.x release.
43
+ Please use `pd.DataFrame.astype` instead.
44
+
45
+ Example: Change the type of a column.
46
+
47
+ >>> import pandas as pd
48
+ >>> import janitor
49
+ >>> df = pd.DataFrame({"col1": range(3), "col2": ["m", 5, True]})
50
+ >>> df
51
+ col1 col2
52
+ 0 0 m
53
+ 1 1 5
54
+ 2 2 True
55
+ >>> df.change_type(
56
+ ... "col1", dtype=str,
57
+ ... ).change_type(
58
+ ... "col2", dtype=float, ignore_exception="fillna",
59
+ ... )
60
+ col1 col2
61
+ 0 0 NaN
62
+ 1 1 5.0
63
+ 2 2 1.0
64
+
65
+ Example: Change the type of multiple columns.
66
+
67
+ Change the type of all columns, please use `DataFrame.astype` instead.
35
68
36
- >>> import pandas as pd
37
- >>> import janitor
38
- >>> df = pd.DataFrame({"col1": range(3), "col2": ["m", 5, True]})
39
- >>> df
69
+ >>> import pandas as pd
70
+ >>> import janitor
71
+ >>> df = pd.DataFrame({"col1": range(3), "col2": ["m", 5, True]})
72
+ >>> df.change_type(['col1', 'col2'], str)
40
73
col1 col2
41
- 0 0 m
42
- 1 1 5
43
- 2 2 True
44
- >>> df.change_type(
45
- ... "col1", dtype=str,
46
- ... ).change_type(
47
- ... "col2", dtype=float, ignore_exception="fillna",
48
- ... )
49
- col1 col2
50
- 0 0 NaN
51
- 1 1 5.0
52
- 2 2 1.0
53
-
54
- Example: Change the type of multiple columns.
55
-
56
- Change the type of all columns, please use `DataFrame.astype` instead.
57
-
58
- >>> import pandas as pd
59
- >>> import janitor
60
- >>> df = pd.DataFrame({"col1": range(3), "col2": ["m", 5, True]})
61
- >>> df.change_type(['col1', 'col2'], str)
62
- col1 col2
63
- 0 0 m
64
- 1 1 5
65
- 2 2 True
66
-
67
- :param df: A pandas DataFrame.
68
- :param column_name: The column(s) in the dataframe.
69
- :param dtype: The datatype to convert to. Should be one of the standard
70
- Python types, or a numpy datatype.
71
- :param ignore_exception: one of `{False, "fillna", "keep_values"}`.
72
- :returns: A pandas DataFrame with changed column types.
73
- :raises ValueError: If unknown option provided for
74
- `ignore_exception`.
74
+ 0 0 m
75
+ 1 1 5
76
+ 2 2 True
77
+
78
+ :param df: A pandas DataFrame.
79
+ :param column_name: The column(s) in the dataframe.
80
+ :param dtype: The datatype to convert to. Should be one of the standard
81
+ Python types, or a numpy datatype.
82
+ :param ignore_exception: one of `{False, "fillna", "keep_values"}`.
83
+ :returns: A pandas DataFrame with changed column types.
84
+ :raises ValueError: If unknown option provided for
85
+ `ignore_exception`.
75
86
"""
76
87
77
88
df = df .copy () # avoid mutating the original DataFrame
0 commit comments