2016-08-09 372 views
0

Ich möchte eine vorhandene DataFrame mit der Ausgabe von einer definierten Routine anfügen, die sich selbst eine DataFrame zurückgibt. Ich möchte sechs der dreizehn Spalten nehmen und den bestehenden Datenrahmen anhängen. Hier ist mein Code die Ausgabe von der definierten Routine zu erstellen:Anfügen eines vorhandenen DataFrame mit Ausgabe von def als DataFrame

import matplotlib.pyplot as plt 
import numpy as np 
import csv 
import pandas 

sqrt_annual = 255**(1/2) 
path = 'data_prices.csv' 
data = pandas.read_csv(path, sep=';', encoding='utf-8-sig') 
data['DATE'] = pandas.to_datetime(data['DATE'], format='%Y%m%d') 
data = data.sort_values(by=['TICKER', 'DATE'], ascending=[True, True]) 

def vol(ticker, date, date_prev, date_next): 
    data_filtered = data[(data.TICKER==ticker) & (data.DATE > date_prev) & (data.DATE < date_next)] 

    data_filtered['pct_change'] = data_filtered.PRICE.pct_change() 
    data_filtered['log_ret'] = np.log(data_filtered.PRICE) - np.log(data_filtered.PRICE.shift(1)) 

    data_filtered['rolling_vola_40'] = sqrt_annual * data_filtered.shift(1).log_ret.rolling(center=False,window=40).std() 
    data_filtered['rolling_vola_80'] = sqrt_annual * data_filtered.shift(1).log_ret.rolling(center=False,window=80).std() 

    data_filtered['f_rolling_vola_40'] = sqrt_annual * data_filtered.shift(-40).log_ret.rolling(center=False,window=40).std() 
    data_filtered['f_rolling_vola_80'] = sqrt_annual * data_filtered.shift(-80).log_ret.rolling(center=False,window=80).std() 


    data_filtered['rolling_vola_prev_annum'] = sqrt_annual * data_filtered[(data.DATE < date)].log_ret.std() 
    data_filtered['rolling_vola_post_annum'] = sqrt_annual * data_filtered[(data.DATE > date)].log_ret.std() 

    prev_x = len(data_filtered[(data_filtered.DATE <= date)])-1 
    post_x = len(data_filtered[(data_filtered.DATE >= date)])-1 

    if prev_x > 235 and post_x > 235: 
     return(data_filtered[(data_filtered.DATE == date)]) 

Zum Beispiel kann der Ausgang für print(vol('A UN Equity','2014-11-17','2013-11-14','2015-11-16')) wäre:

  DATE  TICKER PRICE pct_change log_ret rolling_vola_40 \ 
279 2014-11-17 A UN Equity 41.24 -0.007938 -0.00797   0.253339 

    rolling_vola_80 f_rolling_vola_40 f_rolling_vola_80 \ 
279   0.212863   0.247969   0.241233 

    rolling_vola_prev_annum rolling_vola_post_annum 
279     0.217963     0.225887 

ich dann die DataFrame, die ich angehängt werden soll:

path_static = 'data_static.csv' 
data_static = pandas.read_csv(path_static, sep=';', encoding='utf-8-sig') 
data_static = data_static[(data_static.DATE_PREV != 0) & (data_static.DATE_NEXT != 0)] 

data_static['DATE'] = pandas.to_datetime(data_static['DATE'], format='%Y%m%d') 
data_static['DATE_PREV'] = pandas.to_datetime(data_static['DATE_PREV'], format='%Y%m%d') 
data_static['DATE_NEXT'] = pandas.to_datetime(data_static['DATE_NEXT'], format='%Y%m%d') 

Ich möchte jetzt die letzten sechs Spalten nehmen und meine aktuelle DataFrame anhängen. Die Eingabe für die Funktion ist folgende:

vol(data_static['TICKER'], data_static['DATE'], data_static['DATE_PREV'], data_static['DATE_NEXT']) 

Jeder mit einem Hinweis, wie ich das schaffen kann?

EDIT: Hier sind einige Dummy-Daten.

data_static.csv (einschließlich Header):

YEAR;DATE;TICKER;LONG_COMP_NAME;ISSUER_INDUSTRY;INDUSTRY_SECTOR;COUNTRY;ACCOUNTING_STANDARD;ACCOUNTING_STANDARD_OVERRIDE;EQY_FUND_CRNCY;INDEX;DATE_PREV;DATE_NEXT 
2015;20151116;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20141117;0 
2014;20141117;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20131114;20151116 
2013;20131114;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20121119;20141117 
2012;20121119;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20111115;20131114 
2011;20111115;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20101112;20121119 
2010;20101112;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20091113;20111115 
2009;20091113;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20081114;20101112 
2008;20081114;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20071115;20091113 
2007;20071115;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20061114;20081114 
2006;20061114;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;20051114;20071115 
2005;20051114;A UN Equity;Agilent Technologies Inc;Electronic Measur Instr;Industrial;US;US GAAP;MIXED;USD;S&P500;0;20061114 
2015;20160111;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20150112;0 
2014;20150112;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20140109;20160111 
2013;20140109;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20130108;20150112 
2012;20130108;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20120109;20140109 
2011;20120109;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20110110;20130108 
2010;20110110;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20100111;20120109 
2009;20100111;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20090112;20110110 
2008;20090112;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20080109;20100111 
2007;20080109;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20070109;20090112 
2006;20070109;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;20060109;20080109 
2005;20060109;AA UN Equity;Alcoa Inc;Metal-Aluminum;Basic Materials;US;US GAAP;MIXED;USD;S&P500;0;20070109 

data_prices.csv (einschließlich Header, vorsicht sehr lang):

DATE;TICKER;PRICE 
20151231;A UN Equity;41.81 
20151230;A UN Equity;42.17 
20151229;A UN Equity;42.36 
20151228;A UN Equity;41.78 
20151224;A UN Equity;42.14 
20151223;A UN Equity;41.77 
20151222;A UN Equity;41.22 
20151221;A UN Equity;40.83 
20151218;A UN Equity;40.1 
20151217;A UN Equity;40.78 
20151216;A UN Equity;41.43 
20151215;A UN Equity;40.81 
20151214;A UN Equity;40.25 
20151211;A UN Equity;40.19 
20151210;A UN Equity;41.25 
20151209;A UN Equity;40.98 
20151208;A UN Equity;41.17 
20151207;A UN Equity;40.7 
20151204;A UN Equity;41.1 
20151203;A UN Equity;40.15 
20151202;A UN Equity;40.42 
20151201;A UN Equity;41.06 
20151130;A UN Equity;41.82 
20151127;A UN Equity;41.97 
20151125;A UN Equity;41.34 
20151124;A UN Equity;40.67 
20151123;A UN Equity;40.03 
20151120;A UN Equity;39.28 
20151119;A UN Equity;38.5 
20151118;A UN Equity;39.34 
20151117;A UN Equity;38.38 
20151116;A UN Equity;37.33 
20151113;A UN Equity;36.77 
20151112;A UN Equity;37.49 
20151111;A UN Equity;37.66 
20151110;A UN Equity;37.98 
20151109;A UN Equity;37.92 
20151106;A UN Equity;38.14 
20151105;A UN Equity;38.3 
20151104;A UN Equity;38.34 
20151103;A UN Equity;38.27 
20151102;A UN Equity;38.59 
20151030;A UN Equity;37.76 
20151029;A UN Equity;37.7 
20151028;A UN Equity;37.52 
20151027;A UN Equity;37.05 
20151026;A UN Equity;36.83 
20151023;A UN Equity;37.11 
20151022;A UN Equity;36.09 
20151021;A UN Equity;35.9 
20151020;A UN Equity;36.32 
20151019;A UN Equity;36.23 
20151016;A UN Equity;35.78 
20151015;A UN Equity;35.58 
20151014;A UN Equity;35.05 
20151013;A UN Equity;35.63 
20151012;A UN Equity;35.99 
20151009;A UN Equity;36.23 
20151008;A UN Equity;36.01 
20151007;A UN Equity;35.54 
20151006;A UN Equity;34.9 
20151005;A UN Equity;35.34 
20151002;A UN Equity;34.67 
20151001;A UN Equity;33.74 
20150930;A UN Equity;34.33 
20150929;A UN Equity;33.74 
20150928;A UN Equity;33.37 
20150925;A UN Equity;34.45 
20150924;A UN Equity;34.55 
20150923;A UN Equity;34.95 
20150922;A UN Equity;35.05 
20150921;A UN Equity;35.69 
20150918;A UN Equity;35.74 
20150917;A UN Equity;36.4 
20150916;A UN Equity;36.52 
20150915;A UN Equity;36.15 
20150914;A UN Equity;35.55 
20150911;A UN Equity;35.96 
20150910;A UN Equity;35.86 
20150909;A UN Equity;35.54 
20150908;A UN Equity;36.21 
20150904;A UN Equity;35.06 
20150903;A UN Equity;35.75 
20150902;A UN Equity;35.53 
20150901;A UN Equity;34.75 
20150831;A UN Equity;36.31 
20150828;A UN Equity;36.51 
20150827;A UN Equity;36.64 
20150826;A UN Equity;35.62 
20150825;A UN Equity;34.36 
20150824;A UN Equity;34.68 
20150821;A UN Equity;36.23 
20150820;A UN Equity;37.51 
20150819;A UN Equity;38.4 
20150818;A UN Equity;39.02 
20150817;A UN Equity;38.82 
20150814;A UN Equity;38.65 
20150813;A UN Equity;38.56 
20150812;A UN Equity;38.89 
20150811;A UN Equity;39.42 
20150810;A UN Equity;40.47 
20150807;A UN Equity;39.99 
20150806;A UN Equity;40.12 
20150805;A UN Equity;40.72 
20150804;A UN Equity;40.62 
20150803;A UN Equity;41 
20150731;A UN Equity;40.95 
20150730;A UN Equity;40.97 
20150729;A UN Equity;40.4 
20150728;A UN Equity;40.45 
20150727;A UN Equity;39.61 
20150724;A UN Equity;39.31 
20150723;A UN Equity;40.25 
20150722;A UN Equity;40.33 
20150721;A UN Equity;39.57 
20150720;A UN Equity;40.06 
20150717;A UN Equity;39.95 
20150716;A UN Equity;40.34 
20150715;A UN Equity;40.13 
20150714;A UN Equity;40.49 
20150713;A UN Equity;39.96 
20150710;A UN Equity;39.4 
20150709;A UN Equity;38.92 
20150708;A UN Equity;38.75 
20150707;A UN Equity;39.79 
20150706;A UN Equity;39.36 
20150702;A UN Equity;39.58 
20150701;A UN Equity;39.26 
20150630;A UN Equity;38.58 
20150629;A UN Equity;38.74 
20150626;A UN Equity;40.02 
20150625;A UN Equity;40.05 
20150624;A UN Equity;40.19 
20150623;A UN Equity;39.6 
20150622;A UN Equity;39.81 
20150619;A UN Equity;39.49 
20150618;A UN Equity;39.9 
20150617;A UN Equity;39.6 
20150616;A UN Equity;39.79 
20150615;A UN Equity;39.52 
20150612;A UN Equity;39.84 
20150611;A UN Equity;40.53 
20150610;A UN Equity;40.52 
20150609;A UN Equity;40.12 
20150608;A UN Equity;39.95 
20150605;A UN Equity;40.31 
20150604;A UN Equity;40.54 
20150603;A UN Equity;41.1 
20150602;A UN Equity;41.11 
20150601;A UN Equity;40.92 
20150529;A UN Equity;41.19 
20150528;A UN Equity;41.75 
20150527;A UN Equity;42.61 
20150526;A UN Equity;42.06 
20150522;A UN Equity;42.5 
20150521;A UN Equity;42.32 
20150520;A UN Equity;42.61 
20150519;A UN Equity;42.37 
20150518;A UN Equity;42.63 
20150515;A UN Equity;42.04 
20150514;A UN Equity;42.05 
20150513;A UN Equity;41.81 
20150512;A UN Equity;41.91 
20150511;A UN Equity;42.62 
20150508;A UN Equity;42.5 
20150507;A UN Equity;41.8 
20150506;A UN Equity;41.59 
20150505;A UN Equity;41.59 
20150504;A UN Equity;41.94 
20150430;A UN Equity;41.37 
20150429;A UN Equity;41.96 
20150428;A UN Equity;42.18 
20150427;A UN Equity;41.98 
20150424;A UN Equity;42.49 
20150423;A UN Equity;42.62 
20150422;A UN Equity;42.71 
20150421;A UN Equity;42.89 
20150420;A UN Equity;43.19 
20150417;A UN Equity;42.98 
20150416;A UN Equity;43.13 
20150415;A UN Equity;43.38 
20150414;A UN Equity;43.07 
20150413;A UN Equity;43.04 
20150410;A UN Equity;43.55 
20150409;A UN Equity;42.49 
20150408;A UN Equity;42.26 
20150407;A UN Equity;42.44 
20150402;A UN Equity;42.05 
20150401;A UN Equity;41.39 
20150331;A UN Equity;41.55 
20150330;A UN Equity;41.72 
20150327;A UN Equity;41.11 
20150326;A UN Equity;40.7 
20150325;A UN Equity;40.81 
20150324;A UN Equity;41.09 
20150323;A UN Equity;42.2 
20150320;A UN Equity;42.21 
20150319;A UN Equity;42.21 
20150318;A UN Equity;42.12 
20150317;A UN Equity;41.58 
20150316;A UN Equity;41.81 
20150313;A UN Equity;40.87 
20150312;A UN Equity;41.1 
20150311;A UN Equity;40.85 
20150310;A UN Equity;40.63 
20150309;A UN Equity;41.74 
20150306;A UN Equity;41.53 
20150305;A UN Equity;42.22 
20150304;A UN Equity;42 
20150303;A UN Equity;42.26 
20150302;A UN Equity;42.7 
20150227;A UN Equity;42.21 
20150226;A UN Equity;42.36 
20150225;A UN Equity;42.2 
20150224;A UN Equity;42.06 
20150223;A UN Equity;41.73 
20150220;A UN Equity;41.95 
20150219;A UN Equity;41.15 
20150218;A UN Equity;41.54 
20150217;A UN Equity;40.52 
20150213;A UN Equity;40.15 
20150212;A UN Equity;40.02 
20150211;A UN Equity;39.33 
20150210;A UN Equity;39.67 
20150209;A UN Equity;39.04 
20150206;A UN Equity;39.34 
20150205;A UN Equity;39.53 
20150204;A UN Equity;39.11 
20150203;A UN Equity;39.62 
20150202;A UN Equity;38.69 
20150130;A UN Equity;37.77 
20150129;A UN Equity;38.46 
20150128;A UN Equity;38 
20150127;A UN Equity;38.75 
20150126;A UN Equity;39.15 
20150123;A UN Equity;38.81 
20150122;A UN Equity;39.65 
20150121;A UN Equity;38.16 
20150120;A UN Equity;37.93 
20150116;A UN Equity;38.25 
20150115;A UN Equity;38.01 
20150114;A UN Equity;39.06 
20150113;A UN Equity;39.55 
20150112;A UN Equity;40.11 
20150109;A UN Equity;40.59 
20150108;A UN Equity;40.89 
20150107;A UN Equity;39.7 
20150106;A UN Equity;39.18 
20150105;A UN Equity;39.8 
20150102;A UN Equity;40.56 
20141231;A UN Equity;40.94 
20141230;A UN Equity;41.37 
20141229;A UN Equity;41.33 
20141224;A UN Equity;41.13 
20141223;A UN Equity;41.37 
20141222;A UN Equity;41.88 
20141219;A UN Equity;41.38 
20141218;A UN Equity;40.7 
20141217;A UN Equity;39.76 
20141216;A UN Equity;38.47 
20141215;A UN Equity;38.68 
20141212;A UN Equity;39.72 
20141211;A UN Equity;40.62 
20141210;A UN Equity;40.34 
20141209;A UN Equity;41.4 
20141208;A UN Equity;41.51 
20141205;A UN Equity;42.3 
20141204;A UN Equity;42.27 
20141203;A UN Equity;42.23 
20141202;A UN Equity;41.98 
20141201;A UN Equity;41.59 
20141128;A UN Equity;42.74 
20141126;A UN Equity;42.74 
20141125;A UN Equity;42.71 
20141124;A UN Equity;42.25 
20141121;A UN Equity;42.25 
20141120;A UN Equity;41.26 
20141119;A UN Equity;40.8 
20141118;A UN Equity;40.8 
20141117;A UN Equity;41.24 
20141114;A UN Equity;41.57 
20141113;A UN Equity;41.45 
20141112;A UN Equity;41.45 
20141111;A UN Equity;41.66 
20141110;A UN Equity;41.53 
20141107;A UN Equity;40.93 
20141106;A UN Equity;41.37 
20141105;A UN Equity;40.13 
20141104;A UN Equity;40.18 
20141103;A UN Equity;40.84 
20141031;A UN Equity;39.53 
20141030;A UN Equity;38.8435 
20141029;A UN Equity;38.9722 
20141028;A UN Equity;39.0866 
20141027;A UN Equity;38.6075 
20141024;A UN Equity;38.6504 
20141023;A UN Equity;38.4073 
20141022;A UN Equity;37.9354 
20141021;A UN Equity;38.6147 
20141020;A UN Equity;37.4348 
20141017;A UN Equity;37.3776 
20141016;A UN Equity;36.9771 
20141015;A UN Equity;37.0343 
20141014;A UN Equity;37.0343 
20141013;A UN Equity;37.7494 
20141010;A UN Equity;38.3573 
20141009;A UN Equity;39.3512 
20141008;A UN Equity;40.3738 
20141007;A UN Equity;39.3298 
20141006;A UN Equity;40.4525 
20141003;A UN Equity;40.6956 
20141002;A UN Equity;39.9233 
20141001;A UN Equity;40.1879 
20140930;A UN Equity;40.7456 
20140929;A UN Equity;40.8672 
20140926;A UN Equity;40.4024 
20140925;A UN Equity;40.6813 
20140924;A UN Equity;41.4464 
20140923;A UN Equity;40.7456 
20140922;A UN Equity;41.1532 
20140919;A UN Equity;41.6538 
20140918;A UN Equity;41.7611 
20140917;A UN Equity;42.1115 
20140916;A UN Equity;41.4107 
20140915;A UN Equity;41.4178 
20140912;A UN Equity;41.9041 
20140911;A UN Equity;41.1818 
20140910;A UN Equity;41.2534 
20140909;A UN Equity;41.0817 
20140908;A UN Equity;41.2248 
20140905;A UN Equity;41.4178 
20140904;A UN Equity;41.2534 
20140903;A UN Equity;41.5179 
20140902;A UN Equity;41.5251 
20140829;A UN Equity;40.8744 
20140828;A UN Equity;40.8672 
20140827;A UN Equity;41.0317 
20140826;A UN Equity;41.3678 
20140825;A UN Equity;41.2891 
20140822;A UN Equity;41.2176 
20140821;A UN Equity;41.4679 
20140820;A UN Equity;41.711 
20140819;A UN Equity;41.8755 
20140818;A UN Equity;41.8469 
20140815;A UN Equity;41.1747 
20140814;A UN Equity;39.7731 
20140813;A UN Equity;39.1725 
20140812;A UN Equity;38.8578 
20140811;A UN Equity;39.4728 
20140808;A UN Equity;39.5014 
20140807;A UN Equity;39.3655 
20140806;A UN Equity;39.7517 
20140805;A UN Equity;39.5014 
20140804;A UN Equity;40.2308 
20140801;A UN Equity;40.0949 
20140731;A UN Equity;40.1092 
20140730;A UN Equity;40.6026 
20140729;A UN Equity;40.0663 
20140728;A UN Equity;40.2522 
20140725;A UN Equity;40.4239 
20140724;A UN Equity;40.6455 
20140723;A UN Equity;41.096 
20140722;A UN Equity;41.2748 
20140721;A UN Equity;40.5526 
20140718;A UN Equity;40.1521 
20140717;A UN Equity;39.437 
20140716;A UN Equity;40.7313 
20140715;A UN Equity;40.5168 
20140714;A UN Equity;40.6312 
20140711;A UN Equity;40.4739 
20140710;A UN Equity;40.3953 
20140709;A UN Equity;40.6884 
20140708;A UN Equity;40.8672 
20140707;A UN Equity;41.5608 
20140703;A UN Equity;41.804 
20140702;A UN Equity;41.5108 
20140701;A UN Equity;41.6538 
20140630;A UN Equity;41.0746 
20140627;A UN Equity;41.1175 
20140626;A UN Equity;41.3463 
20140625;A UN Equity;41.4536 
20140624;A UN Equity;41.2248 
20140623;A UN Equity;41.5537 
20140620;A UN Equity;42.0257 
20140619;A UN Equity;41.9184 
20140618;A UN Equity;42.3832 
20140617;A UN Equity;42.0972 
20140616;A UN Equity;41.7039 
20140613;A UN Equity;41.8326 
20140612;A UN Equity;41.7611 
20140611;A UN Equity;42.1258 
20140610;A UN Equity;42.1115 
20140609;A UN Equity;42.3903 
20140606;A UN Equity;42.1973 
20140605;A UN Equity;41.9041 
20140604;A UN Equity;41.0817 
20140603;A UN Equity;40.9173 
20140602;A UN Equity;40.6813 
20140530;A UN Equity;40.717 
20140529;A UN Equity;40.7313 
20140528;A UN Equity;40.8386 
20140527;A UN Equity;40.2451 
20140523;A UN Equity;40.1736 
20140522;A UN Equity;39.7159 
20140521;A UN Equity;39.3226 
20140520;A UN Equity;38.8364 
20140519;A UN Equity;39.3083 
20140516;A UN Equity;39.3512 
20140515;A UN Equity;38.9651 
20140514;A UN Equity;39.9376 
20140513;A UN Equity;40.6384 
20140512;A UN Equity;40.338 
20140509;A UN Equity;39.6087 
20140508;A UN Equity;39.5443 
20140507;A UN Equity;39.3512 
20140506;A UN Equity;39.3369 
20140505;A UN Equity;39.5658 
20140502;A UN Equity;39.0008 
20140430;A UN Equity;38.6433 
20140429;A UN Equity;38.1141 
20140428;A UN Equity;38.4216 
20140425;A UN Equity;38.8936 
20140424;A UN Equity;39.53 
20140423;A UN Equity;39.3369 
20140422;A UN Equity;39.3083 
20140417;A UN Equity;39.0795 
20140416;A UN Equity;38.3859 
20140415;A UN Equity;37.9783 
20140414;A UN Equity;37.6493 
20140411;A UN Equity;37.7351 
20140410;A UN Equity;38.6504 
20140409;A UN Equity;39.7374 
20140408;A UN Equity;39.0866 
20140407;A UN Equity;38.865 
20140404;A UN Equity;39.7374 
20140403;A UN Equity;40.4739 
20140402;A UN Equity;40.524 
20140401;A UN Equity;40.3023 
20140331;A UN Equity;39.9877 
20140328;A UN Equity;39.1081 
20140327;A UN Equity;39.1081 
20140326;A UN Equity;39.4513 
20140325;A UN Equity;39.4442 
20140324;A UN Equity;39.5157 
20140321;A UN Equity;40.1092 
20140320;A UN Equity;40.5454 
20140319;A UN Equity;40.4954 
20140318;A UN Equity;40.6098 
20140317;A UN Equity;39.9018 
20140314;A UN Equity;39.6444 
20140313;A UN Equity;39.9376 
20140312;A UN Equity;40.8029 
20140311;A UN Equity;40.6598 
20140310;A UN Equity;41.5823 
20140307;A UN Equity;41.7754 
20140306;A UN Equity;42.0257 
20140305;A UN Equity;41.332 
20140304;A UN Equity;41.3177 
20140303;A UN Equity;40.5526 
20140228;A UN Equity;40.7099 
20140227;A UN Equity;40.3023 
20140226;A UN Equity;40.8315 
20140225;A UN Equity;40.6813 
20140224;A UN Equity;40.5812 
20140221;A UN Equity;40.288 
20140220;A UN Equity;40.9673 
20140219;A UN Equity;40.002 
20140218;A UN Equity;39.437 
20140214;A UN Equity;39.5085 
20140213;A UN Equity;42.9624 
20140212;A UN Equity;42.8265 
20140211;A UN Equity;42.7121 
20140210;A UN Equity;42.1973 
20140207;A UN Equity;42.4905 
20140206;A UN Equity;41.6824 
20140205;A UN Equity;41.0674 
20140204;A UN Equity;41.3392 
20140203;A UN Equity;40.1521 
20140131;A UN Equity;41.5823 
20140130;A UN Equity;42.5548 
20140129;A UN Equity;41.2248 
20140128;A UN Equity;41.5465 
20140127;A UN Equity;41.6896 
20140124;A UN Equity;41.3821 
20140123;A UN Equity;42.5262 
20140122;A UN Equity;43.5702 
20140121;A UN Equity;43.513 
20140117;A UN Equity;43.4129 
20140116;A UN Equity;43.2628 
20140115;A UN Equity;43.1483 
20140114;A UN Equity;42.8194 
20140113;A UN Equity;42.1401 
20140110;A UN Equity;42.1401 
20140109;A UN Equity;41.7682 
20140108;A UN Equity;41.7539 
20140107;A UN Equity;41.0817 
20140106;A UN Equity;40.5025 
20140103;A UN Equity;40.7027 
20140102;A UN Equity;40.195 
20131231;A UN Equity;40.8958 
20131230;A UN Equity;40.9888 
20131227;A UN Equity;40.8815 
20131224;A UN Equity;40.9244 
20131223;A UN Equity;41.1461 
20131220;A UN Equity;40.7313 
20131219;A UN Equity;41.0317 
20131218;A UN Equity;41.3964 
20131217;A UN Equity;40.3094 
20131216;A UN Equity;39.5014 
20131213;A UN Equity;39.4442 
20131212;A UN Equity;39.5157 
20131211;A UN Equity;39.2154 
20131210;A UN Equity;39.5729 
20131209;A UN Equity;39.3727 
20131206;A UN Equity;39.2082 
20131205;A UN Equity;38.2571 
20131204;A UN Equity;38.3144 
20131203;A UN Equity;37.8495 
20131202;A UN Equity;38.0498 
20131129;A UN Equity;38.3072 
20131127;A UN Equity;38.4788 
20131126;A UN Equity;38.3644 
20131125;A UN Equity;37.9783 
20131122;A UN Equity;38.5074 
20131121;A UN Equity;38.6862 
20131120;A UN Equity;38.2786 
20131119;A UN Equity;38.486 
20131118;A UN Equity;38.6504 
20131115;A UN Equity;39.2797 
20131114;A UN Equity;36.1405 
20131113;A UN Equity;36.6124 
20131112;A UN Equity;36.6124 
20131111;A UN Equity;36.684 
20131108;A UN Equity;36.3121 
20131107;A UN Equity;35.7901 
20131106;A UN Equity;36.498 
20131105;A UN Equity;36.6196 
20131104;A UN Equity;36.6196 
20131101;A UN Equity;36.5409 
20131031;A UN Equity;36.2978 
20131030;A UN Equity;36.5767 
20131029;A UN Equity;36.97 
20131028;A UN Equity;36.8198 
20131025;A UN Equity;37.0916 
20131024;A UN Equity;36.5552 
20131023;A UN Equity;36.2191 
20131022;A UN Equity;36.4837 
20131021;A UN Equity;37.1774 
20131018;A UN Equity;37.8924 
20131017;A UN Equity;37.6493 
20131016;A UN Equity;36.8699 
20131015;A UN Equity;36.3836 
20131014;A UN Equity;36.7269 
20131011;A UN Equity;36.7984 
20131010;A UN Equity;36.4623 
20131009;A UN Equity;35.7186 
20131008;A UN Equity;35.8473 
20131007;A UN Equity;36.5624 
20131004;A UN Equity;37.0272 
+0

Ich empfehle das Lesen von [docs - Merge, join und concatenate] (http://pandas.pydata.org/pandas-docs/st fable/merging.html # concatenating-objects), es ist alles, was Sie brauchen – Anzel

+0

Ich bekomme die DataFrames zu verschmelzen, aber nicht die Funktion, vor zu laufen. Das ist mein Problem. Ich denke, ich sollte eine Art von Iteration verwenden, aber nicht sicher, ob dies wirklich notwendig ist. Ich werde später dann 'pandas.merge (data_static, Daten, wie = 'left', on = ['TICKER', 'DATE'])' 'verwenden. Dies sollte reibungslos funktionieren. Aber ich muss zuerst 'data_output' definieren. Idealerweise wäre dies: 'data_output = vol (data_static ['TICKER'], data_static ['DATE'], data_static ['DATE_PREV'], data_static ['DATE_NEXT']) '. Aber das ist leider nicht der richtige Weg. – Spurious

+0

Sie können die Funktion wahrscheinlich nur mit sort + indexing zB erreichen. 'df.ix [: 6,:]'. Finde den gemeinsamen "Index" zwischen 2 Datenrahmen, von denen ich denke, dass sie "DATE" sind, dann führe einfach eine Zusammenführung durch – Anzel

Antwort

0

konnte ich die Lösung die folgende Art und Weise erhalten:

data_store = pandas.DataFrame(columns=('TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum')) 

for index, row in data_static.iterrows(): 
    data_output = vol(row['TICKER'], row['DATE'], row['DATE_PREV'], row['DATE_NEXT']) 
    if type(data_output) != type(None): 
     data_store = data_store.append(data_output[['TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum']]) 

data_static = pandas.merge(data_static, data_store[['TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum']], how='left', on=['TICKER', 'DATE']) 
data_static.to_csv('test.csv', sep=';', encoding='utf-8') 
+0

Es ist nicht sehr schnell, also könnte es eine bessere Lösung geben. – Spurious