简体   繁体   English

Python时间戳作为索引

[英]Python timestamp as index

as Im writing my masterthesis at the moment I have to work a with Python for the first time.因为我现在正在写我的硕士论文,所以我必须第一次使用 Python。 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.好吧,也许确实如此,但是通过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提前致谢

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()

在此处输入图片说明

All df:所有 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM