Dengar
Dengar

Reputation: 464

Error adding date column in Pandas

Needed some help solving why my dataframe is returning all NaNs.

print df
   0  1  2  3   4
0  1  9  0  7  30
1  2  8  0  4  30
2  3  5  0  3  30
3  4  3  0  3  30
4  5  1  0  3  30

Then I added date index. I only need to increment by one day for 5 days.

date = pd.date_range(datetime.datetime.today(), periods=5)
data = DataFrame(df, index=date)
print data

                             0   1   2   3   4
2014-04-10 17:16:09.433000 NaN NaN NaN NaN NaN
2014-04-11 17:16:09.433000 NaN NaN NaN NaN NaN
2014-04-12 17:16:09.433000 NaN NaN NaN NaN NaN
2014-04-13 17:16:09.433000 NaN NaN NaN NaN NaN
2014-04-14 17:16:09.433000 NaN NaN NaN NaN NaN

Tried a few different things to no avail. If I switch my original dataframe to

 np.random.randn(5,5)

Then it works. Anyone have an idea of what is going on here?

Edit: Going to add that the data type is float64

print df.dtypes

0    float64
1    float64
2    float64
3    float64
4    float64
dtype: object

Upvotes: 1

Views: 84

Answers (1)

Blaszard
Blaszard

Reputation: 31965

You should overwrite the index of the original dataframe with the following:

df.index = date

What DataFrame(df, index=date) does is that it creates new dataframe by matching the values of index to the df being used, for example:

DataFrame(df, index=[0,1,2,5,5])

returns the following:

    0   1   2   3   4
0   1   9   0   7  30
1   2   8   0   4  30
2   3   5   0   3  30
5 NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN

because 5 is not included in the index of the original dataframe.

Upvotes: 1

Related Questions