从日期时间列表中的日期帧中索引数据时间

时间:2017-11-14 16:27:22

标签: python pandas time

首先,我做了这个函数,它返回一个日期框架,但我想在日期列表中使用,然后在一个数据框中连接它们,索引是列表中的日期时间戳

{headerName: 'Country Group - No Renderer', field:'year', showRowGroup: 'year'},
{headerName: "Country", field: "country", width: 120, rowGroup: true},
{headerName: "Year", field: "year", width: 90, rowGroup: true},
{headerName: "Gold", field: "gold", width: 100, aggFunc: 'sum'},
{headerName: "Silver", field: "silver", width: 100, aggFunc: 'sum'},
{headerName: "Bronze", field: "bronze", width: 100, aggFunc: 'sum'}

这是我的功能:

lista = [datetime.datetime(2017, 11, 11, 0, 0), datetime.datetime(2017, 11, 12, 0, 0), datetime.datetime(2017, 11, 13, 0, 0)] 

现在从一个看起来像这样:

def min_f(yyear,mmonth,dday):

    a_00_04 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,4)+timedelta(hours=2))) ].min() ) #.tolist()[0]#
    a_04_08 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,4,00)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,8,00)+timedelta(hours=2))) ].min() )
    a_08_12 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,8,00)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,12,00)+timedelta(hours=2))) ].min() )
    a_12_16 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,12,00)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,16,00)+timedelta(hours=2))) ].min() )
    a_16_20 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,16,00)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,20,00)+timedelta(hours=2))) ].min() )
    a_20_24 = int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,20,00)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday+1,00,00)+timedelta(hours=2))) ].min() )

    d = {'00_04': [a_04_08], '04_08': [a_04_08], '08_12': [a_08_12], '12_16': [a_12_16],'20_24': [a_20_24]}
    df = pd.DataFrame(data=d)

    return df

我想这样设置:

   00_04  04_08  08_12  12_16  20_24
0  21359  21359  10486   6747  14335

但是还添加了我的列表中的结果

1 个答案:

答案 0 :(得分:1)

不是积极的你想要的,但也许是这样的:

def create_df(dl):
    idx = []
    cols = {
        '00_04': [],
        '04_08': [],
        '08_12': [],
        '12_16': [],
        '20_24': [],
    }

    for date in dl:
        col['00_04'].append(int( df_output.loc[ (df_output.index > timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday)-timedelta(hours=1)))  &  (df_output.index <= timezone('Europe/Berlin').localize(datetime(yyear,mmonth,dday,4)+timedelta(hours=2))) ].min() ))
        ...

        index.append(date)

    return pd.DataFrame(cols, index=idx)

很难说出你的目的是什么,但似乎你可能也错过了16-24字段?

希望这有帮助。