How to change index dtype of pandas DataFrame to int32?

后端 未结 3 666
甜味超标
甜味超标 2020-12-11 03:47

A default dtype of DataFrame index is int64 and I would like to change it to int32.

I tried changing it with pd.DataFrame.set_index and Num

3条回答
  •  一个人的身影
    2020-12-11 04:11

    All of the code paths I could find, coerce the dtype:

    Check in pandas.Index.__new__()

    if issubclass(data.dtype.type, np.integer):
        from .numeric import Int64Index
        return Int64Index(data, copy=copy, dtype=dtype, name=name)
    

    This allows passing a dtype, but in NumericIndex().__new__() we have:

    if copy or not is_dtype_equal(data.dtype, cls._default_dtype):
        subarr = np.array(data, dtype=cls._default_dtype, copy=copy)
    

    Which changes the dtype.

提交回复
热议问题