Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按年度调整时间序列_Python_Pandas - Fatal编程技术网

Python 按年度调整时间序列

Python 按年度调整时间序列,python,pandas,Python,Pandas,是否有一种更短或更优雅的方式来在熊猫身上以年为轴心旋转时间序列?下面的代码符合我的要求,但我想知道是否有更好的方法来实现这一点: import pandas import numpy daterange = pandas.date_range(start='2000-01-01', end='2017-12-31', freq='10T') # generate a fake timeseries of measured wind speeds from 2000 to 2017 in 10m

是否有一种更短或更优雅的方式来在熊猫身上以年为轴心旋转时间序列?下面的代码符合我的要求,但我想知道是否有更好的方法来实现这一点:

import pandas
import numpy

daterange = pandas.date_range(start='2000-01-01', end='2017-12-31', freq='10T')
# generate a fake timeseries of measured wind speeds from 2000 to 2017 in 10min intervals
wind_speed = pandas.Series(data=numpy.random.rand(daterange.size), index=daterange)
# group by year
wind_speed_groups = wind_speed.groupby(wind_speed.index.year).groups
# assemble data frame with columns of wind speed data for every year
wind_speed_pivot = pandas.DataFrame()
for key, group in wind_speed_groups.items():
    series = wind_speed[group]
    series.name = key
    series.index = series.index - pandas.Timestamp(str(key)+'-01-01')
    wind_speed_pivot = wind_speed_pivot.join(series, how='outer')
print(wind_speed_pivot)

我不确定这是否是最快的方法,因为我正在向初始数据帧添加两列(如果要覆盖它,可以只添加一列)


我投票决定以离题的方式结束这个问题,因为它属于CodeReview。@Deena:运行脚本,你会得到应有的结果。。大表,列名从“2000”到“2017”(年份),索引从“0天00:00:00”到“365天00:00:00”(时间差),充满了风速数据。谢谢!在这种情况下,速度并不重要。
import pandas as pd
import numpy as np
import datetime as dt
daterange = pd.date_range(start='2000-01-01', end='2017-12-31', freq='10T')
# generate a fake timeseries of measured wind speeds from 2000 to 2017 in 10min intervals
wind_speed = pd.Series(data=np.random.rand(daterange.size), index=daterange)

df = wind_speed.to_frame("windspeed")
df["year"] = df.index.year
df["pv_index"] = df.index - df["year"].apply(lambda x: dt.datetime(x,1,1))
wind_speed_pivot = df.pivot_table(index=["pv_index"], columns=["year"], values=["windspeed"])