pandas 1.4.2

NotesParametersReturnsBackRef
convert_dtypes(self: 'NDFrameT', infer_objects: 'bool_t' = True, convert_string: 'bool_t' = True, convert_integer: 'bool_t' = True, convert_boolean: 'bool_t' = True, convert_floating: 'bool_t' = True) -> 'NDFrameT'
versionadded

Notes

By default, convert_dtypes will attempt to convert a Series (or each Series in a DataFrame) to dtypes that support pd.NA . By using the options convert_string , convert_integer , convert_boolean and convert_boolean , it is possible to turn off individual conversions to StringDtype , the integer extension types, BooleanDtype or floating extension types, respectively.

For object-dtyped columns, if infer_objects is True , use the inference rules as during normal Series/DataFrame construction. Then, if possible, convert to StringDtype , BooleanDtype or an appropriate integer or floating extension type, otherwise leave as object .

If the dtype is integer, convert to an appropriate integer extension type.

If the dtype is numeric, and consists of all integers, convert to an appropriate integer extension type. Otherwise, convert to an appropriate floating extension type.

versionchanged

Starting with pandas 1.2, this method also converts float columns to the nullable floating extension type.

In the future, as new dtypes are added that support pd.NA , the results of this method will change to support those new dtypes.

Parameters

infer_objects : bool, default True

Whether object dtypes should be converted to the best possible types.

convert_string : bool, default True

Whether object dtypes should be converted to StringDtype() .

convert_integer : bool, default True

Whether, if possible, conversion can be done to integer extension types.

convert_boolean : bool, defaults True

Whether object dtypes should be converted to BooleanDtypes() .

convert_floating : bool, defaults True

Whether, if possible, conversion can be done to floating extension types. If :None:None:`convert_integer` is also True, preference will be give to integer dtypes if the floats can be faithfully casted to integers.

versionadded

Returns

Series or DataFrame

Copy of input object with new dtype.

Convert columns to best possible dtypes using dtypes supporting pd.NA .

See Also

infer_objects

Infer dtypes of objects.

to_datetime

Convert argument to datetime.

to_numeric

Convert argument to a numeric type.

to_timedelta

Convert argument to timedelta.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame(
...  {
...  "a": pd.Series([1, 2, 3], dtype=np.dtype("int32")),
...  "b": pd.Series(["x", "y", "z"], dtype=np.dtype("O")),
...  "c": pd.Series([True, False, np.nan], dtype=np.dtype("O")),
...  "d": pd.Series(["h", "i", np.nan], dtype=np.dtype("O")),
...  "e": pd.Series([10, np.nan, 20], dtype=np.dtype("float")),
...  "f": pd.Series([np.nan, 100.5, 200], dtype=np.dtype("float")),
...  }
... )

Start with a DataFrame with default dtypes.

This example is valid syntax, but we were not able to check execution
>>> df
   a  b      c    d     e      f
0  1  x   True    h  10.0    NaN
1  2  y  False    i   NaN  100.5
2  3  z    NaN  NaN  20.0  200.0
This example is valid syntax, but we were not able to check execution
>>> df.dtypes
a      int32
b     object
c     object
d     object
e    float64
f    float64
dtype: object

Convert the DataFrame to use best possible dtypes.

This example is valid syntax, but we were not able to check execution
>>> dfn = df.convert_dtypes()
... dfn a b c d e f 0 1 x True h 10 <NA> 1 2 y False i <NA> 100.5 2 3 z <NA> <NA> 20 200.0
This example is valid syntax, but we were not able to check execution
>>> dfn.dtypes
a      Int32
b     string
c    boolean
d     string
e      Int64
f    Float64
dtype: object

Start with a Series of strings and missing data represented by np.nan .

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(["a", "b", np.nan])
... s 0 a 1 b 2 NaN dtype: object

Obtain a Series with dtype StringDtype .

This example is valid syntax, but we were not able to check execution
>>> s.convert_dtypes()
0       a
1       b
2    <NA>
dtype: string
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.infer_objects

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#6132
type: <class 'function'>
Commit: