Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change index dtype of pandas DataFrame to int32?

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 NumPy array of int32, also tried making new index with dtype=np.int32. It didn't work, always returning index of int64.

Can someone show a working code to produce Pandas index with int32 size?

I use conda Pandas v0.20.1.

like image 879
Stanpol Avatar asked May 20 '17 21:05

Stanpol


1 Answers

Can someone show a working code to produce pandas index with int32 size?

@PietroBattiston's answer may work. But it's worth explaining why you should ordinarily not want to replace the default RangeIndex with an Int64 / Int32 index.

Storing the logic behind a range of values takes less memory than storing each integer in a range. This should be clear when you compare, for instance, Python's built-in range with NumPy np.arange. As described in the pd.RangeIndex docs:

RangeIndex is a memory-saving special case of Int64Index limited to representing monotonic ranges. Using RangeIndex may in some instances improve computing speed.

like image 180
jpp Avatar answered Sep 27 '22 17:09

jpp