1

I'm trying to generate a period of time stamps from 21 august to 23 oct. Each time stamps has a time delta of 15 minutes.

The problem with current code is some days are missing. Can't find a logical problem here. The data is too much to debug.

hours_counter = 0
minutes_counter = 0
days_counter = 0

for j in range(30):
    days_counter = days_counter + 1
    if days_counter > 30:
        days_counter = 0

    for i in range(360):
        minutes_counter = minutes_counter + 15
        if minutes_counter == 45:
            minutes_counter = 0
            hours_counter = hours_counter + 1

        s = datetime.strptime('2018-08-21', '%Y-%m-%d') + timedelta(hours=hours_counter, 
                                                                    minutes=minutes_counter,
                                                                    days=days_counter)
        timeStamps.append(s)
        path = os.path.join(
            r'C:\\ahmed\\SpeedFT-meter12\\peakdata\\' + s.strftime("%Y-%m-%d") + r'\\peakdata_' + s.strftime(
                "%Y%m%d_%H%M") + r'.bz2')
        directoary_names.append(path)
Johan
  • 3,577
  • 1
  • 14
  • 28
andre
  • 731
  • 2
  • 13
  • 27
  • Which days are missing? Have you tried reducing the range and/or increasing the time delta to try tracking down the problem? – lxop Nov 07 '18 at 09:14
  • 2 of sept is just one time stamp, 12 of oct is just one time stamp. – andre Nov 07 '18 at 09:16
  • 2
    Why not just keeping adding a 15-minute timedelta to the most recent timestamp, greatly simplifying the logic? – NPE Nov 07 '18 at 09:17
  • 1
    I advise you to use [Pandas' `pd.daterange()`](https://stackoverflow.com/a/23190286/10484131) – Felix Nov 07 '18 at 09:19

2 Answers2

2

One problem is that when you move to the next day, you don't reset the hours counter back to zero (there could be other).

Personally, I'd simplify the logic a great deal and write:

import datetime

ts = datetime.datetime.strptime('2018-08-21', '%Y-%m-%d')
end_ts = datetime.datetime.strptime('2018-10-24', '%Y-%m-%d')
while ts < end_ts:
  print(ts)
  ts += datetime.timedelta(minutes=15)
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Also that you are doing 90 hours worth of 15-minute advances before advancing by a day, so I would expect big gaps in the series every 3 days and 18 hours – lxop Nov 07 '18 at 09:21
0

As an alternative for people looking here. You can just use the pandas data_range function to generate the list between the start_date and end_date.

import pandas as pd
start_date = '2018-08-21'
end_date =  '2018-10-24'
alltimes=pd.date_range(start_date, end_date, freq='15min')
for times in alltimes:
    print times
MEdwin
  • 2,940
  • 1
  • 14
  • 27