0

I have stock data in a .csv file. The date column is in unix time. How do I convert the entire column to readable dates? Also, how do I set the x axis to day-times and not seconds? Very new to this..

RBZ one day.csv looks like this: RBZ csv data

df = pd.read_csv('RBZ one day.csv')
fig = plt.figure(figsize=(15,7))
ax1 = plt.subplot2grid((40,40), (0,0), rowspan=40, colspan=40)

ax1.plot(open)
fredo124
  • 67
  • 1
  • 2
  • 7
  • The convertion to unix time is discussed in this question: https://stackoverflow.com/questions/26763344/convert-pandas-column-to-datetime – j_stroebel Jan 23 '20 at 16:09

1 Answers1

1

Your datetime column is in Unix time to the millisecond, so use this to convert it:

df['datetime'] = pd.to_datetime(df['datetime'],unit='ms')

Then you can plot using ax.plot_date():

ax1.plot_date(df['datetime'],df['open'])

Example:

import matplotlib.dates as mdates

df = pd.DataFrame({'datetime':[1575293880000,1575294180000,1575294420000,1575297000000,1575297060000],
                   'open':[np.random.random() for i in range(5)]})
fig = plt.figure(figsize=(5,5))
ax = fig.gca()
ax.plot_date(df['datetime'],df['open'])
ax.xaxis.set_major_locator(mdates.HourLocator())
fig.autofmt_xdate() 

Results in:

enter image description here

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223