1

I am working on a real estate cash-flow simulation.

What I want in the end is a time series where everyday I report if the property is vacant, leased and if I collected rent.

In my present code, I create first a profit array with values of "Leased", "Vacant" or "Today you collected rent of $1000", so I used this to create my time series:

rng=pd.date_range('6/1/2016', periods=len(profit), freq='D')
ts=pd.Series(profit, index=rng)

To simplify, I assumed I collected rent every 30 days. Now I want to be more specific and collect it every 5th day of the month (for example) and be flexible on the day the next tenant will move in.

Do you know commands or a good source where I can learn how to iterate from month to month?

Any help would be appreciated

minocha
  • 1,043
  • 1
  • 12
  • 26
  • https://docs.python.org/2/library/datetime.html, and, http://stackoverflow.com/questions/546321/how-do-i-calculate-the-date-six-months-from-the-current-date-using-the-datetime – tenwest May 24 '16 at 20:49
  • I looked at both references but still I can't figure it out. Let's say I have a time series and I want to enter the value "1000" to a random day and every month after that for a year. – Daniele Agostino D'Apuzzo May 24 '16 at 21:23
  • Sorry, a pandas person should chime in. I could do it with a regular date time, and the random module for the random day -- by basically looping through 12 months. I figured datetime would be compatible with a pandas timeseries..but I know nothing about pandas! – tenwest May 24 '16 at 23:39

1 Answers1

0

You can build a sequence of dates using date_range and .shift() (freq='M' is for month-end frequencies) with pd.datetools.day like so:

date_sequence = pd.date_range(start, end, freq='M').shift(num_of_days, freq=pd.datetools.day)

and then use this sequence to select dates from the DateTimeIndex using

df.loc[date_sequence, 'column_name'] = value

Alternatively, you can use pd.DateOffset() like so:

ts = pd.date_range(start=date(2015, 6, 1), end=date(2015, 12, 1), freq='MS')

DatetimeIndex(['2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01',
           '2015-10-01', '2015-11-01', '2015-12-01'],
          dtype='datetime64[ns]', freq='MS')

Now add 5 days:

ts + pd.DateOffset(days=5)

to get:

DatetimeIndex(['2015-06-06', '2015-07-06', '2015-08-06', '2015-09-06',
               '2015-10-06', '2015-11-06', '2015-12-06'],
              dtype='datetime64[ns]', freq=None)
Stefan
  • 41,759
  • 13
  • 76
  • 81
  • This is perfect. I used your date_sequence along with date_sequence.union() to create a time series that concatenates time series starting at different points and with different lengths of time. Thanks! – Daniele Agostino D'Apuzzo May 25 '16 at 16:29
  • Is there anyway to create a time series from June 6th to December 6th (for example) without using .shift()? The reason I ask this is that by using shift iteratively, I end up with small mistakes, like : Feb 11th, March 14th, April 12th, May 13th and so forth so it doesn't "stick" to the day. The error is minor, probably negligible but I wondered if I could make it exact. Thanks! – Daniele Agostino D'Apuzzo May 25 '16 at 22:30
  • That kinda works. Let me explain. The number of days that I shift by is in my code increasing. Let's say I start with shifting 8 days, then 16...as soon as I hit 32 it shifts by 2 months and a day instead of a single month and a day. It seems like when I enter pd.DateOffset(days=32) it reads it as pd.DateOffset(months=1, days=32) – Daniele Agostino D'Apuzzo May 27 '16 at 20:57
  • Maybe you could ask a new question and show in more detail what your current code and desired output look like? I've been trying to answer your original question but we don't seem to be getting to a conclusion here. – Stefan May 27 '16 at 21:28
  • I am sorry, your help was amazing. I will post another question hoping to make it clearer what the situation is. Hopefully you will be able to reply to it – Daniele Agostino D'Apuzzo May 31 '16 at 14:41