1

I have a list of date which I create using the function date_range of pandas. In that list, there are dates from 01 January 2019 00:00:00 to 31 December 2019 00:00:00. Now there is gap of 15 mins in each time interval, so in a day there will be a total of 96 15mins blocks((24*60)/15). So list will be like:

modelStartDate = "01/01/2019"
modelEndDate = "31/12/2019"
interval = 15
dateTime = pd.date_range(start=modelStartDate, end=modelEndDate, freq=str(interval) + 'min')

print(dateTime)

output:

DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 00:15:00',
               '2019-01-01 00:30:00', '2019-01-01 00:45:00',
               '2019-01-01 01:00:00', '2019-01-01 01:15:00',
               '2019-01-01 01:30:00', '2019-01-01 01:45:00',
               '2019-01-01 02:00:00', '2019-01-01 02:15:00',
               ...
               '2019-12-30 21:45:00', '2019-12-30 22:00:00',
               '2019-12-30 22:15:00', '2019-12-30 22:30:00',
               '2019-12-30 22:45:00', '2019-12-30 23:00:00',
               '2019-12-30 23:15:00', '2019-12-30 23:30:00',
               '2019-12-30 23:45:00', '2019-12-31 00:00:00'],
              dtype='datetime64[ns]', length=34945, freq='15T')

So I am doing some calculations which requires some counters and what I want to do is that I want to reset the counter on every new day. So do this task I have two ways: first, where I can simply divide the list into 96 blocks and reset the counter after every 96th block and second, I want to go through this list and see in actual when is the new days and reset the counter only when the it found that there is a new day. first approach:

modelStartDate = "01/01/2019"
modelEndDate = "31/12/2019"
interval = 15
dateTime = pd.date_range(start=modelStartDate, end=modelEndDate, freq=str(interval) + 'min')

for i in range(len(dateTime)):
    if i % 96 == 0:
        print("new date at ", i)

second approach: I don't know how to approach that. So can someone please help. In the desired output I don't want anything particular, just can simply tell that that's the new date on a new day. So after every 96th block output will look like:

new date: 02/01/2019 00:00:00
new date: 03/ 01/2019 00:00:00
and so on
Vesper
  • 795
  • 1
  • 9
  • 21

3 Answers3

3

You could use the Timestamp.round() function to compare the datetime value to its rounded counterpart and if it's true the counter resets.

modelStartDate = "01/01/2019"
modelEndDate = "31/12/2019"
interval = 15
dateTime = pd.date_range(start=modelStartDate, end=modelEndDate, freq=str(interval) + 'min')

counter = 0
for i in dateTime:
    # Do your calculations
    counter+=1  # Counter value incrementing

    if i == i.round(freq='D'):  # Check if its the start of the day
        counter = 0  # Reset the counter
        print("New Date at {}".format(i))
Varun Rai
  • 46
  • 3
1

you could make use of the dayofyear method of the DatetimeIndex. Whenever it changes, you have a new day. Ex:

import pandas as pd

modelStartDate, modelEndDate = "01/01/2019", "31/12/2019"
interval = 15
dateTime = pd.date_range(start=modelStartDate, end=modelEndDate, freq=str(interval) + 'min')

doy = pd.Series(dateTime.dayofyear)
m = doy.ne(doy.shift())

dateTime[m]
# DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
#                '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
#                '2019-01-09', '2019-01-10',
#                ...
#                '2019-12-22', '2019-12-23', '2019-12-24', '2019-12-25',
#                '2019-12-26', '2019-12-27', '2019-12-28', '2019-12-29',
#                '2019-12-30', '2019-12-31'],
#               dtype='datetime64[ns]', length=365, freq=None)

to get the 'counter' you describe, you can make use of Cumsum reset at NaN, modify it a little bit and use

v = (~m).astype(int)
cumsum = v.cumsum()
reset = -cumsum[v == 0].diff().fillna(cumsum)
counting = v.where(v != 0, reset).cumsum()
# counting
# 0         0
# 1         1
# 2         2
# 3         3
# 4         4
#          ..
# 34940    92
# 34941    93
# 34942    94
# 34943    95
# 34944     0
# Length: 34945, dtype: int64
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
1

This works for me, but maybe not so pandas-like:

modelStartDate = "01/01/2019"
modelEndDate = "31/12/2019"
interval = 15
dateTime = pd.date_range(start=modelStartDate, end=modelEndDate)
dateTime_min = pd.date_range(start=modelStartDate, end=modelEndDate, freq=str(interval) + 'min')
df_combined = pd.DataFrame({"date_frequency":dateTime_min})
df_final = pd.DataFrame()
for date in dateTime:
    df_final = df_final.append(df_combined[df_combined['date_frequency'].dt.strftime('%d/%m/%Y') == date.strftime('%d/%m/%Y')].reset_index())
    display(df_final)

Output:

    index   date_frequency
0     0     2019-01-01 00:00:00
1     1     2019-01-01 00:15:00
2     2     2019-01-01 00:30:00
3     3     2019-01-01 00:45:00
4     4     2019-01-01 01:00:00
...     ...     ...
91  187     2019-01-02 22:45:00
92  188     2019-01-02 23:00:00
93  189     2019-01-02 23:15:00
94  190     2019-01-02 23:30:00
95  191     2019-01-02 23:45:00
user2853437
  • 750
  • 8
  • 27