0
votes

I am working with intraday stock data that i have downloaded to a CSV file. The data contains the stock price for MO per minute. In order to generate a time-frame for the data, i use the pandas function:

pd.timedelta_range('1 days 9 hours 30 minutes', periods=len(df), freq='min')

To add the two dataframes togehter, i use the following

time = pd.DataFrame(data = df,index=pd.timedelta_range('1 days 9 hours 30 minutes', periods=len(df), freq='min'))

It results in this

                 MO
1 days 09:30:00 NaN
1 days 09:31:00 NaN
1 days 09:32:00 NaN
1 days 09:33:00 NaN
1 days 09:34:00 NaN

Not sure why i am getting NaN values for the stock data.

Raw data (df) looks like this:

MO
65.67
65.74
66.064
65.99
65.8801
65.87
65.89
65.9
65.73
65.67
...
...
1

1 Answers

4
votes

If you have a dataframe df containing MO then you can use set_index i.e

df = df.set_index(pd.timedelta_range('1 days 9 hours 30 minutes', periods=len(df), freq='min'))

Output :

                      MO
1 days 09:30:00  65.6700
1 days 09:31:00  65.7400
1 days 09:32:00  66.0640
1 days 09:33:00  65.9900
1 days 09:34:00  65.8801
1 days 09:35:00  65.8700
1 days 09:36:00  65.8900
1 days 09:37:00  65.9000
1 days 09:38:00  65.7300
1 days 09:39:00  65.6700