2

I have a dF that has dates as its index, and I would like to add a new column 'delta' that shows the number of days between the date in the index and todays date.

I have:

dF['today'] = '2015-01-30'
dF['today'] = pd.to_datetime(dF['today'])
dF['tvalue']= dF.index
dF['delta'] = (dF['tvalue'] - dF['today'])

Obviously, this is not working. Any feedback?

headdetective
  • 367
  • 1
  • 4
  • 11

2 Answers2

2
import time
import datetime

def ymd2epoch(y, m, d):
    t = datetime.datetime(y, m, d, 0, 0, 0).timetuple()
    return int(time.mktime(t))

def yyyymmdd2epoch(s):
    ymd = map(int, s.split('-'))
    return ymd2epoch(ymd[0], ymd[1], ymd[2])

def ymd_todaydiff(ymd):
    seconds_diff = int(time.time()) - yyyymmdd2epoch(ymd)
    daydiff = seconds_diff / (24*60*60)
    return daydiff

if __name__ == '__main__':
    import sys

    for ymd in sys.argv[1:]:
        sys.stdout.write("DayDiff(%s) = %d\n" % 
                         (ymd, ymd_todaydiff(ymd)))
    sys.exit(0)

running it:

$ ./daydiff.py 2015-01-01 2015-01-29 2015-01-30 2014-01-30
DayDiff(2015-01-01) = 30
DayDiff(2015-01-29) = 2
DayDiff(2015-01-30) = 1
DayDiff(2014-01-30) = 366
Yotam
  • 808
  • 8
  • 10
2

You can use pandas methods to solve the problem right away.

Consider e.g.

df = pd.DataFrame({'A' : [1,2,3]}
      , index = [pd.to_datetime(['2015-01-30', '2015-01-29','2015-01-25'])] )

that is

                 A
     2015-01-30  1
     2015-01-29  2
     2015-01-25  3

then you define

today = pd.to_datetime('2015-01-30')

so that you can finally calculate

(df.index - today).days

 array([ 0, -1, -5])

which clearly can be added as a further column.


Finally, the date of today can be obtained automatically by doing

today = pd.datetime.today()
Acorbe
  • 8,367
  • 5
  • 37
  • 66