datetime - Calculating time until end of year python -


i have stock price data every weekday past 50 years, including date/timestamp. @ end of each month need calculate remaining days until end of year. leap years must considered since solution has use time stamp. tried code:

from datetime import datetime  t = df.index dfa =(datetime(t.year, 12, 31) -t)/(datetime(t.year, 12, 31)-datetime(t.year, 1, 1)) 

however, returns typeerror:

only length-1 arrays can converted python scalars

i tried this:

i=0 df["time_value"] = 0 while i<len(df):     t = df.index[i]     time_value=(datetime(t.year, 12, 31) -t)/(datetime(t.year, 12, 31)-datetime(t.year, 1, 1))     df.time_value[i] = time_value     i+1 

this short example of dataframe used:

              close_spx    close_iboxx  a_returns  b_returns  a_vola    b_vola 2014-05-15    1870.85      234.3017    -0.009362   0.003412   0.170535  0.075468    2014-05-16    1877.86      234.0216     0.003747  -0.001195   0.170153  0.075378 2014-05-19    1885.08      233.7717     0.003845  -0.001068   0.170059  0.075384    2014-05-20    1872.83      234.2596    -0.006498   0.002087   0.170135  0.075410    2014-05-21    1888.03      233.9101     0.008116  -0.001492   0.169560  0.075326    2014-05-22    1892.49      233.5429     0.002362  -0.001570   0.169370  0.075341    2014-05-23    1900.53      233.8605     0.004248   0.001360   0.168716  0.075333    2014-05-27    1911.91      234.0368     0.005988   0.000754   0.168797  0.075294    2014-05-28    1909.78      235.4454    -0.001114   0.006019   0.168805  0.075474    2014-05-29    1920.03      235.1813     0.005367  -0.001122   0.168866  0.075451    2014-05-30    1923.57      235.2161     0.001844   0.000148   0.168844  0.075430    2014-06-02    1924.97      233.8868     0.000728  -0.005651   0.168528  0.075641    2014-06-03    1924.24      232.9049    -0.000379  -0.004198   0.167852  0.075267 

pandas has dateoffset capability simplifies such calculation. can access yearend offset pd.datetools.yearend(). create new column 'd' work on. each value in column add yearend offset. subtract original date sum give number of days end of year:

import pandas pd df['d'] = df.index  df['days'] = df.apply(lambda i: i['d'] + pd.datetools.yearend() - i['d'], axis=1) 

you don't need create new 'days' column, can overwrite column 'd' if wish.

print df['days'] yield:

2014-05-15   230 days 2014-05-16   229 days 2014-05-19   226 days 2014-05-20   225 days 2014-05-21   224 days 2014-05-22   223 days 2014-05-23   222 days 2014-05-27   218 days 2014-05-28   217 days 2014-05-29   216 days 2014-05-30   215 days 2014-06-02   212 days 2014-06-03   211 days 

if yourindex not in datetime format convert pd.to_datetime or use parse_dates=true when load file.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -