Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python timestamp as index

Tags:

as Im writing my masterthesis at the moment I have to work a with Python for the first time. In order to index my data with a timestamp i tried the following which does not really work. Well maybe it does but Iam to stupid to acess the data through the timestemp. Maybe someone can help me to do the next step so that I get acces to the data using the timestamp so that I can slice my yearly data into months for example.

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from matplotlib import pyplot
import datetime as dt
from matplotlib.pylab import rcParams
import datetime
rcParams['figure.figsize'] = 15, 6

data = pd.read_csv('PhelixPowerSpotHistory_2015.csv')
data['Delivery Date']=pd.to_datetime(data['Delivery Date'])
#data['Time']= pd.to_datetime(data.DeliveryDate)
#print (data.head())
print(data.head(10))
from datetime import datetime
ts = data['PriceEUR/MWh']
print(ts.head(10))


import matplotlib.pyplot as plt
plt.plot(ts)
plt.ylabel('€/MWh')
plt.xlabel('Delivery Date')
plt.xticks(np.linspace(0,8721,12))
plt.show()

The head of my data looks like this:

0 2015-01-01 00:00:00         25.02
1 2015-01-01 01:00:00         18.29
2 2015-01-01 02:00:00         16.04
3 2015-01-01 03:00:00         14.60
4 2015-01-01 04:00:00         14.95
5 2015-01-01 05:00:00         14.50
6 2015-01-01 06:00:00         10.76
7 2015-01-01 07:00:00         12.01
8 2015-01-01 08:00:00         12.39
9 2015-01-01 09:00:00         14.04

Thanks in advance

like image 368
Felix Frölich Avatar asked Nov 14 '18 15:11

Felix Frölich


1 Answers

It is not really clear to me what is your desired output, but to acces to data by the date, you can do in this way:

df['delivery date'] = pd.to_datetime(df['delivery date']) # convert column to datetime object
df.set_index('delivery date', inplace=True) # set column 'date' to index

To access to the data of a day:

print (df.loc['2015-01-01 00:00:00']) 

Output:

€/MWh    25.02

And to plot:

df.plot()
plt.show()

enter image description here

All df:

                       €/MWh
delivery date               
2015-01-01 00:00:00    25.02
2015-01-01 01:00:00    18.29
2015-01-01 02:00:00    16.04
2015-01-01 03:00:00    14.60
2015-01-01 04:00:00    14.95
2015-01-01 05:00:00    14.50
2015-01-01 06:00:00    10.76
2015-01-01 07:00:00    12.01
2015-01-01 08:00:00    12.39
2015-01-01 09:00:00    14.04
like image 85
Joe Avatar answered Oct 12 '22 00:10

Joe