rearThing
rearThing

Reputation: 632

Pandas dates not showing on plot

My dataframe is from a cvs as follow:

         Date     Open     High     Low    Close  Adj Close    Volume
0  1996-01-01  4.06250  4.12500  3.8750  3.90625   3.093209   7048800
1  1996-02-01  3.84375  3.96875  3.5000  3.62500   2.870497  12864000
2  1996-03-01  3.50000  4.25000  3.5000  4.12500   3.266428   9526400
3  1996-04-01  4.06250  4.68750  4.0625  4.50000   3.563378   5693600
4  1996-05-01  4.40625  4.65625  4.1250  4.21875   3.340666  30480000

When I try to plot it there is no date on the bottom of the graph. Here is the code I am using:

df = pd.read_csv('ABC.csv') #read cvs
df2 = df.loc[0:12] #select rows
df2.set_index('Date', inplace=True) #set Date as index
df2['Close'].plot() #plot

enter image description here

Upvotes: 2

Views: 5043

Answers (2)

CallMeCrazy
CallMeCrazy

Reputation: 132

Set your column date as datetime type :)

df = pd.read_csv('ABC.csv') #read cvs
df2 = df.loc[0:12] #select rows
df2['Date'] = pd.to_datetime(df2['Date'])
df2.set_index('Date', inplace=True) #set Date as index
df2['Close'].plot() #plot

Upvotes: 2

ALollz
ALollz

Reputation: 59579

Your date is a string, so matplotlib so it wont automatically plot the labels. Change it to a datetime first and then plot.

import pandas as pd
df['Date'] = pd.to_datetime(df.Date)

Your plot:

df2 = df.loc[0:12]
df2.set_index('Date', inplace=True)
df2['Close'].plot()

enter image description here

Upvotes: 4

Related Questions