5

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!

2
  • Try setting the 'Date' column as the index with df.set_index (...) Commented Aug 30, 2016 at 14:46
  • Or put df as first argument then specify the time and value kwargs accordingly. Commented Aug 30, 2016 at 14:48

3 Answers 3

9

Not sure that tsplot is the best tool for that. You can just use:

df[['Date','Value']].set_index('Date').plot()
Sign up to request clarification or add additional context in comments.

2 Comments

this is simple and working, and yet one may want to note that if not all consecutive dates exist in the data, this will not generate an axis continuous/linear to the passing of time.
One could use the resample() to get a continuous axis.
2

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'])

Comments

2

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.