Home > Software design >  date/time: increment the datetime column by one second for each subset of dataframe
date/time: increment the datetime column by one second for each subset of dataframe

Time:11-20

creating subset of dataframe and changing the datetime column by 1 day bt not incrementing in the seconds. showing one single value for each one. following code I had written:

st= Timestamp('2018-06-18 07:59:20')
startDate = st

for labour in range(2):
    for trip in range(np.random.randint(5,7)):
#         np.random.seed(42)
        temp_df = redf[:3000].copy()
        temp_df['labor_id'] = labour
        
        temp_df['DateTime'] = startDate
        for i in range(len(temp_df)): 
            temp_df['DateTime']  = datetime.timedelta(seconds=1)
            
        startDate  = datetime.timedelta(days=1, minutes=12, seconds = 3)
        print(temp_df['DateTime'].head(3))

# output
0   2018-06-18 08:49:20
1   2018-06-18 08:49:20
2   2018-06-18 08:49:20
Name: DateTime, dtype: datetime64[ns]
0   2018-06-19 09:01:23
1   2018-06-19 09:01:23
2   2018-06-19 09:01:23
Name: DateTime, dtype: datetime64[ns]
0   2018-06-20 09:13:26
1   2018-06-20 09:13:26
2   2018-06-20 09:13:26
Name: DateTime, dtype: datetime64[ns]
0   2018-06-21 09:25:29
1   2018-06-21 09:25:29
2   2018-06-21 09:25:29
Name: DateTime, dtype: datetime64[ns]
0   2018-06-22 09:37:32
1   2018-06-22 09:37:32
2   2018-06-22 09:37:32

It gives the single one value for each temp_df . want to increment by one seconds each

Desired Output

# output
0   2018-06-18 08:49:20
1   2018-06-18 08:49:21
2   2018-06-18 08:49:22
Name: DateTime, dtype: datetime64[ns]
0   2018-06-19 09:01:23
1   2018-06-19 09:01:24
2   2018-06-19 09:01:25
Name: DateTime, dtype: datetime64[ns]
0   2018-06-20 09:13:26
1   2018-06-20 09:13:27
2   2018-06-20 09:13:28
Name: DateTime, dtype: datetime64[ns]
0   2018-06-21 09:25:29
1   2018-06-21 09:25:30
2   2018-06-21 09:25:31
Name: DateTime, dtype: datetime64[ns]
0   2018-06-22 09:37:32
1   2018-06-22 09:37:33
2   2018-06-22 09:37:34

How to do that

CodePudding user response:

You can use timedelta_range for add incremental timedeltas:

redf = pd.DataFrame({'a':range(5)})

startDate = pd.Timestamp('2018-06-18 07:59:20')


for labour in range(2):

    for trip in range(np.random.randint(5,7)):
#         np.random.seed(42)
        temp_df = redf[:3].copy()
        temp_df['labor_id'] = labour
        
        td = pd.timedelta_range('1s', periods=len(temp_df), freq='1s')
        temp_df['DateTime'] = startDate   td
            
        startDate = temp_df['DateTime'].max()
        print(temp_df['DateTime'].head(3))
        startDate  = datetime.timedelta(days=1, minutes=12, seconds = 3)
---
        
14062   2018-06-18 11:45:00
14063   2018-06-18 11:45:01
14064   2018-06-18 11:45:02
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-19 15:42:45
14063   2018-06-19 15:42:46
14064   2018-06-19 15:42:47
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-20 19:40:30
14063   2018-06-20 19:40:31
14064   2018-06-20 19:40:32
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-21 23:38:15
14063   2018-06-21 23:38:16
14064   2018-06-21 23:38:17
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-23 03:36:00
14063   2018-06-23 03:36:01
14064   2018-06-23 03:36:02
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-24 07:33:45
14063   2018-06-24 07:33:46
14064   2018-06-24 07:33:47
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-25 11:31:30
14063   2018-06-25 11:31:31
14064   2018-06-25 11:31:32
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-26 15:29:15
14063   2018-06-26 15:29:16
14064   2018-06-26 15:29:17
Name: DateTime, dtype: datetime64[ns]
14062   2018-06-27 19:27:00
14063   2018-06-27 19:27:01
14064   2018-06-27 19:27:02
Name: DateTime, dtype: datetime64[ns]
  • Related