Plotting time stamp column versus another column in Python

fahad pirzada Source

I am .xls file and i have column of timestamp with following format of timestamp

   2018-04-01 00:01:45
   2018-04-01 00:16:45
   2018-04-01 00:31:46
   2018-04-01 00:46:45
   2018-04-01 01:01:46
   2018-04-01 01:16:45
   2018-04-01 01:31:50
   2018-04-01 01:46:45
   2018-04-01 02:01:46

I Have another column with in same .xls file by name of temperature with following format

34
34
34
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33

I want to plot values versus time . I tried to plot it but i am having issues in plotting it as it is not correctly reading the timestamp

My code :

#changing timestamp data from object to datatype
    w = df['Timestamp']
   // column name "Timestamp" was creating issue so i have to remove it"
    w=w.drop(w.index[0])
    //converting timestamp type object to datetime
    w = pd.to_datetime(w)

    area = (12 * np.random.rand(N))**2  # 0 to 15 point radii
    plt.xlabel('Temperature')
    plt.ylabel('DateTime')
    plt.title('Temperature and DateTime Relation')
    plt.scatter(t, w, s=area, c='purple', alpha=0.5)
    plt.show()

Its Giving me error "TypeError: invalid type promotion"

pythonpython-3.xpandas

Answers

answered 6 months ago jezrael #1

I believe need to_datetime with parameter format first and then for 15Min data add resample with some function like mean:

df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y %H:%M')

s = df.resample('15Min', on='date')['temp'].mean()

s.plot()

comments powered by Disqus