0

I have a date array

date_array = array(['September 11, 2012', 'September 5, 2012'])

I want to graph this date_array against a value, value_array =array([25,28]) using matplotlib

So I want to convert the date_array to a number like

date_as_number=array([20120812, 20120805])

so that I could plot the date_as_number as any other number against the value_array.

1

2 Answers 2

0

To Parse Dates

from dateutil.parser import parse

date_array = np.array(['September 11, 2012', 'September 5, 2012'])
[parse(x).date() for x in date_array]

Output:

[datetime.date(2012, 9, 11), datetime.date(2012, 9, 5)]

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. That really helped me.
0

Set the date column as index and use the pandas builtin plot to get the date wise plot that you need. Below is an example

import random
import pandas as pd
data = [('September {}, 2012'.format(i),random.random()) for i in range(1,30)]
data_df = pd.DataFrame(data,columns=['_date','value'])
data_df['_date'] = pd.to_datetime(data_df._date)
data_df= data_df.set_index('_date')
data_df.head()

This gives a dataframe like this

            value
_date   
2012-09-01  0.379791
2012-09-02  0.146355
2012-09-03  0.545267
2012-09-04  0.398388
2012-09-05  0.926643

Just plot this df.

 data_df.plot()

enter image description here

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.