0

Struggling with something that should be easy:

today = '26/8/2018'
start = '1/8/2018'
diff = today - start

diff gives us 26 days

how do I take the integer value of this datetime? i.e. 26?

basically, im trying to calc a daycount fraction, (diff / 365) * 10,000 say, but it wont work.

My actual values I have are:

0       304.548
1       371.397
2       350.466
3      -3574.36
4       255.452

and im trying to multiply them by: duration

0     13 days      
1     2 days       
2     1 days         
3     20 days       
4      7 days 

But I get:

0       TimedeltaIndex(['3959 days 02:57:32.054794',  ...
1       TimedeltaIndex([ '4828 days 03:56:42.739725', ...
2       TimedeltaIndex([ '4556 days 01:18:54.246575', ...
3       TimedeltaIndex(['-46467 days +08:52:36.164383'...
4       TimedeltaIndex(['3320 days 21:02:27.945204',  ...

desired output is 0 3959.124 as an integer (304.548*13), not as a daycount

Junaid Mohammad
  • 457
  • 1
  • 6
  • 18
  • [This](https://stackoverflow.com/questions/32991934/equivalent-function-of-datenumdatestring-of-matlab-in-python) question is relevant. – jodag Aug 26 '18 at 17:52
  • Why are you trying to do `diff/365 * 10000`? A years isn't 365 days, it's 365 or 366 depending on the year, or 365.2425 days on average, – abarnert Aug 26 '18 at 18:11
  • In financial math, interest rates are calculated using actual days/365 or sometimes 30/360 (so assumptions are built in). For my example, I am happy to use 365 days per year. However, despite Ashish's solution working as expected, my code wont work.. ugh.. I have a series of time deltas, e.g. Timedelta('13 days 00:00:00') but when multiplying by 304.548, I get another timedelta: TimedeltaIndex(['3959 days 02:57:32.054794', ... and the entire series of timedeltas doesn't recognise ".days" statistics['duration'].days gives an error: "'Series' object has no attribute 'days'" – Junaid Mohammad Aug 26 '18 at 18:20

1 Answers1

2

Perhaps something like this might work:

In [1]: import datetime

In [4]: diff = datetime.datetime.today() - datetime.datetime(year=2018, month=8, day=1)

In [5]: diff.days
Out[5]: 25

Then you can do something like:

In [10]: diff.days / 365 * 10000
Out[10]: 684.931506849315
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25