Reputation: 2533
I have a csv file with some time series data. I create a Data Frame as such:
df = pd.read_csv('C:\\Desktop\\Scripts\\TimeSeries.log')
When I call df.head(6)
, the data appears as follows:
Company Date Value
ABC 08/21/16 00:00:00 500
ABC 08/22/16 00:00:00 600
ABC 08/23/16 00:00:00 650
ABC 08/24/16 00:00:00 625
ABC 08/25/16 00:00:00 675
ABC 08/26/16 00:00:00 680
Then, I have the following to force the 'Date' column into datetime format:
df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')
Interestingly, I see "pandas.core.series.Series
" when I call the following:
type(df['Date'])
Finally, I call the following to create a plot:
%matplotlib qt
sns.tsplot(df['Value'])
On the x-axis from left to right, I see integers ranging from 0 to the number of rows in the data frame. How does one add the 'Date' column as the x-axis values to this plot?
Thanks!
Upvotes: 5
Views: 50613
Reputation: 2052
use the time
parameter for tsplot
from docs:
time : string or series-like
Either the name of the field corresponding to time in the data DataFrame or x values for a plot when data is an array. If a Series, the name will be used to label the x axis.
#Plot the Value column against Date column
sns.tsplot(data = df['Value'], time = df['Date'])
However tsplot
is used to plot timeseries in the same time window for different conditions. To plot a single timeseries you could also use plt.plot(time = df['Date'], data = df['Value'])
Upvotes: 2
Reputation: 347
I think it is too late.
First, you have to notice that 'Date' column is a series of 'datetime' type so you should do that to get the 'date' part:
df['Date'] = df['Date'].map(lambda x:x.date())
now group your data frame by 'Date' and then reset index in order to make 'Date' a column (not an index).
Then you can use plt.plot_date
df_groupedby_date = df.groupby('Date').count()
df_groupedby_date.reset_index(inplace=True)
plt.plot_date(x=df_groupedby_date['Date'], y=df_groupedby_date['Value'])
Upvotes: 2
Reputation: 3930
Not sure that tsplot is the best tool for that. You can just use:
df[['Date','Value']].set_index('Date').plot()
Upvotes: 9