Int64Index with dtype=float and slicing issues · Issue #9966 · pandas-dev/pandas (original) (raw)

I have stumbled onto problems with slicing a pandas DataFrame when an index that originally contained integers has been manipulated such that it contains floats. Consider the following example (somewhat contrived to make it stand on its own; in practice, data are loaded from file):

df = pd.DataFrame({'i': np.linspace(0, 5, 6).astype(np.int64), 
               'j': np.linspace(2, 3, 6)})
df.set_index('i', inplace=True)
print(df.index)   # Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')
df.index = df.index / 10.
print(df.index)   # Int64Index([0.0, 0.1, 0.2, 0.3, 0.4, 0.5], dtype='float64')
df[:0.3]

The final line raises this exception:

TypeError: the slice stop value [None] is not a proper indexer 
for this index type (Int64Index)

While it was relatively easy to work around this using an explicit cast along the lines of

df.index = np.asarray(df.index, dtype=np.float64) / 10.

it took me quite a while to figure out what was going on. What I expected to happen in the original code was that the index would become a Float64Index or something similar. When I first noticed the Int64Index with dtype=float I thought that was wierd. Is it a good idea to change this behaviour so that there is not this mismatch between index type and dtype?

My pandas version is 0.15.2. User EdChum at StackOverflow[1] has kindly tested this on 0.16, finding the same behaviour with the code above. Slicing with .ix works on 0.16 but not on 0.15.2; slicing with .loc works for both versions.

[1] http://stackoverflow.com/questions/29795225/pandas-int64index-with-dtype-float