Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.astype#

DataFrame.astype(dtype,copy=None,errors='raise')[source]#

Cast a pandas object to a specified dtypedtype.

Parameters:
dtypestr, data type, Series or Mapping of column name -> data type

Use a str, numpy.dtype, pandas.ExtensionDtype or Python type tocast entire pandas object to the same type. Alternatively, use amapping, e.g. {col: dtype, …}, where col is a column label and dtype isa numpy.dtype or Python type to cast one or more of the DataFrame’scolumns to column-specific types.

copybool, default True

Return a copy whencopy=True (be very careful settingcopy=False as changes to values then may propagate to otherpandas objects).

Note

Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.

You can already get the future behavior and improvements throughenabling copy on writepd.options.mode.copy_on_write=True

errors{‘raise’, ‘ignore’}, default ‘raise’

Control raising of exceptions on invalid data for provided dtype.

  • raise : allow exceptions to be raised

  • ignore : suppress exceptions. On error return original object.

Returns:
same type as caller

See also

to_datetime

Convert argument to datetime.

to_timedelta

Convert argument to timedelta.

to_numeric

Convert argument to a numeric type.

numpy.ndarray.astype

Cast a numpy array to a specified type.

Notes

Changed in version 2.0.0:Usingastype to convert from timezone-naive dtype totimezone-aware dtype will raise an exception.UseSeries.dt.tz_localize() instead.

Examples

Create a DataFrame:

>>>d={'col1':[1,2],'col2':[3,4]}>>>df=pd.DataFrame(data=d)>>>df.dtypescol1    int64col2    int64dtype: object

Cast all columns to int32:

>>>df.astype('int32').dtypescol1    int32col2    int32dtype: object

Cast col1 to int32 using a dictionary:

>>>df.astype({'col1':'int32'}).dtypescol1    int32col2    int64dtype: object

Create a series:

>>>ser=pd.Series([1,2],dtype='int32')>>>ser0    11    2dtype: int32>>>ser.astype('int64')0    11    2dtype: int64

Convert to categorical type:

>>>ser.astype('category')0    11    2dtype: categoryCategories (2, int32): [1, 2]

Convert to ordered categorical type with custom ordering:

>>>frompandas.api.typesimportCategoricalDtype>>>cat_dtype=CategoricalDtype(...categories=[2,1],ordered=True)>>>ser.astype(cat_dtype)0    11    2dtype: categoryCategories (2, int64): [2 < 1]

Create a series of dates:

>>>ser_date=pd.Series(pd.date_range('20200101',periods=3))>>>ser_date0   2020-01-011   2020-01-022   2020-01-03dtype: datetime64[ns]

[8]ページ先頭

©2009-2025 Movatter.jp