9

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

1 Answer 1

11

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
Sign up to request clarification or add additional context in comments.

6 Comments

So what if I want to separate for example the first 3 values. Intuitively i would try something like this which didnt work : df1= df['2015-01-01 00:00:00':'2015-01-01 02:00:00']
What do you mean with 'separate the values'? This:print df.iloc[:3,:]?
Well I have a dataset with consists of 8000 values within one year and I want to ha a look at the months for example
To access by name and not index, use .loc instead of .iloc
For the second question of month, please open a new question where you describe exactly the output you wanted :)
|

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.