pandas.Index.astype#
- Index.astype(dtype, copy=True)[源代码]#
创建具有转换为 dtypes 的值的 Index。
新 Index 的类由 dtype 决定。当转换不可能时,会引发 TypeError 异常。
- Parameters:
- dtypenumpy dtype 或 pandas 类型
请注意,任何有符号整数 dtype 都被视为
'int64',任何无符号整数 dtype 都被视为'uint64',无论其大小如何。- copybool, default True
默认情况下,astype 始终返回新分配的对象。如果将 copy 设置为 False,并且满足 dtype 的内部要求,则使用原始数据创建新的 Index 或者返回原始 Index。
- Returns:
- pandas.DataFrame.keys
将值强制转换为指定 dtype 的 Index。
Examples
>>> idx = pd.Index([1, 2, 3]) >>> idx Index([1, 2, 3], dtype='int64') >>> idx.astype('float') Index([1.0, 2.0, 3.0], dtype='float64')