1

I'm currently getting a panda data frame from alpaca - a stock trading platform. The data I'm getting is for minute data and is giving me a timestamp that cant be converted to DateTime to make my functions easier. Heres an output for AMZN minute data

                             open       high        low    
timestamp                                                                   
2019-08-08 14:12:00-04:00  1826.8600  1826.9649  1826.1026  
2019-08-08 14:13:00-04:00  1826.6500  1827.6520  1826.6500  

Heres the data type:

print(type(AMZN.index[0])) 
<class'pandas._libs.tslibs.timestamps.Timestamp'>

I'm trying to split this index into two columns where the day is the index and the time(minutes and hours) is the next column. Or just getting it into all into datetime format together would work fine too.

I've tried finding a similar problem and using _datetime() but it doesn't seem to work. I have the datetime library but it doesn't seem to register when I use the code as in other posts.

Overall Im just trying to find an easy way of iterating over a certain time on any given day and this format is giving me trouble. If someone has a better way of dealing with time series of this type please feel free to share.

  • Sounds like a duplicate of https://stackoverflow.com/q/466345/940217 -- I would just take the string value of this Timestamp object and parse it using datetime.strptime – Kyle Falconer Sep 06 '19 at 23:23

1 Answers1

0

Actually Timestamp is one of the easiest data type to manipulate in pandas:

df['Date'] = df.index.date
df['Time'] = df.index.time
df = df.set_index(df['Date])

Result:

               open       high        low      Time
Date                                               
2019-08-08  1826.86  1826.9649  1826.1026  14:12:00
2019-08-08  1826.65  1827.6520  1826.6500  14:13:00
Code Different
  • 90,614
  • 16
  • 144
  • 163