pandas 1.4.2

NotesParametersReturns
astype(self: 'NDFrameT', dtype, copy: 'bool_t' = True, errors: 'str' = 'raise') -> 'NDFrameT'

Notes

deprecated

Using astype to convert from timezone-naive dtype to timezone-aware dtype is deprecated and will raise in a future version. Use :None:meth:`Series.dt.tz_localize` instead.

Parameters

dtype : data type, or dict of column name -> data type

Use a numpy.dtype or Python type to cast entire pandas object to the same type. Alternatively, use {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 when copy=True (be very careful setting copy=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.

Returns

casted : same type as caller

Cast a pandas object to a specified dtype dtype .

See Also

numpy.ndarray.astype

Cast a numpy array to a specified type.

to_datetime

Convert argument to datetime.

to_numeric

Convert argument to a numeric type.

to_timedelta

Convert argument to timedelta.

Examples

Create a DataFrame:

This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> df.astype('int32').dtypes
col1    int32
col2    int32
dtype: object

Cast col1 to int32 using a dictionary:

This example is valid syntax, but we were not able to check execution
>>> df.astype({'col1': 'int32'}).dtypes
col1    int32
col2    int64
dtype: object

Create a series:

This example is valid syntax, but we were not able to check execution
>>> ser = pd.Series([1, 2], dtype='int32')
... ser 0 1 1 2 dtype: int32
This example is valid syntax, but we were not able to check execution
>>> ser.astype('int64')
0    1
1    2
dtype: int64

Convert to categorical type:

This example is valid syntax, but we were not able to check execution
>>> ser.astype('category')
0    1
1    2
dtype: category
Categories (2, int64): [1, 2]

Convert to ordered categorical type with custom ordering:

This example is valid syntax, but we were not able to check execution
>>> from pandas.api.types import CategoricalDtype
... cat_dtype = CategoricalDtype(
...  categories=[2, 1], ordered=True)
... ser.astype(cat_dtype) 0 1 1 2 dtype: category Categories (2, int64): [2 < 1]

Note that using copy=False and changing data on a new pandas object may propagate changes:

This example is valid syntax, but we were not able to check execution
>>> s1 = pd.Series([1, 2])
... s2 = s1.astype('int64', copy=False)
... s2[0] = 10
... s1 # note that s1[0] has changed too 0 10 1 2 dtype: int64

Create a series of dates:

This example is valid syntax, but we were not able to check execution
>>> ser_date = pd.Series(pd.date_range('20200101', periods=3))
... ser_date 0 2020-01-01 1 2020-01-02 2 2020-01-03 dtype: datetime64[ns]
See :

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#5749
type: <class 'function'>
Commit: