I have a pandas dataframe with index 3 to 15 with 0.5 steps and want to reindex it to 0.1 steps. I tried this code and it doesn't work
# create data and set index and print for verification
df = pd.DataFrame({'A':np.arange(3,5,0.5),'B':np.arange(3,5,0.5)})
df.set_index('A', inplace = True)
df.reindex(np.arange(3,5,0.1)).head(15)
The above code outputs this:
A | B |
---|---|
3.0 | 3.0 |
3.1 | NaN |
3.2 | NaN |
3.3 | NaN |
3.4 | NaN |
3.5 | NaN * expected output in this position to be 3.5 since it exists in the original df |
3.6 | NaN |
3.7 | NaN |
3.8 | NaN |
Strangely the problem is fixed when reindexing from 0 instead of 3 as it's shown in the code below:
df = pd.DataFrame({'A':np.arange(3,5,0.5),'B':np.arange(3,5,0.5)})
df.set_index('A', inplace = True)
print(df.head())
df.reindex(np.arange(0,5,0.1)).head(60)
The output now correctly shows
A | B |
---|---|
0.0 | NaN |
... | ... |
3.0 | 3.0 |
3.1 | NaN |
3.2 | NaN |
3.3 | NaN |
3.4 | NaN |
3.5 | 3.5 |
3.6 | NaN |
3.7 | NaN |
3.8 | NaN |
I'm running python 3.8.5 on Windows 10.
Pandas version is 1.4.07
Numpy version is 1.22.1
Does anyone know why this happens? If it's a known or new bug? If the bug has been fixed in a newer version of python, pandas or numpy?
Thanks