如何将pandas DataFrame的索引dtype更改为int32? [英] How to change index dtype of pandas DataFrame to int32?
本文介绍了如何将pandas DataFrame的索引dtype更改为int32?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataFrame索引的默认dtype是int64
,我想将其更改为int32
.
A default dtype of DataFrame index is int64
and I would like to change it to int32
.
我尝试使用 int32
的NumPy数组,也尝试使用dtype=np.int32
创建新索引.它不起作用,总是返回int64
的索引.
I tried changing it with pd.DataFrame.set_index
and NumPy array of int32
, also tried making new index with dtype=np.int32
. It didn't work, always returning index of int64
.
有人可以显示一个工作代码来生成大小为int32
的熊猫索引吗?
Can someone show a working code to produce Pandas index with int32
size?
我使用的是conda Pandas v0.20.1.
I use conda Pandas v0.20.1.
推荐答案
不确定这在实践中是否值得做,但是以下方法应该可以工作:
Not sure this is something worth doing in practice, but the following should work:
class Int32Index(pd.Int64Index):
_default_dtype = np.int32
@property
def asi8(self):
return self.values
i = Int32Index(np.array([...], dtype='int32'))
(摘自此处)
这篇关于如何将pandas DataFrame的索引dtype更改为int32?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文