Home > Enterprise >  How to generate date time column values with time values for every minute interval in python
How to generate date time column values with time values for every minute interval in python

Time:08-05

I have now written this code to generate fake values for the columns. I want the datetime column with values like, for a particular day for every minute the data should be generated, so for one day (24 * 60) values should be there. this is the code:

import pandas as pd
import numpy as np
import faker

# create some fake data
fake = Faker()

# function to create a dataframe with fake values for our workers
def make_workers(num):
    
    # lists to randomly assign to workers
    status_list = ['in', 'out']
    team_list = ['FL1_RM1','FL1_RM2','FL1_RM3', 'FL1_RM4', 'FL2_RM1']
    d1 = datetime.strptime(f'1/1/2022', '%m/%d/%Y')
    d2 = datetime.strptime(f'08/08/2022', '%m/%d/%Y')

    fake_workers = [{'ID':fake.random_number(digits=10),
                  'Temp':np.random.randint(low=35, high=50), 
                  'noted Date':fake.date_time_between(d1,d2),
                  'Status':np.random.choice(status_list), # assign items from list with different probabilities
                  'room_id':np.random.choice(team_list)} for x in range(num)]
        
    return fake_workers

worker_df = pd.DataFrame(make_workers(num=50000))
worker_df.head(20)

this will generate random time values for any particular date. Is there a way to generate date time values for every minute for each day.

CodePudding user response:

import pandas as pd
import numpy as np
import faker

fake = Faker()

def make_workers():
    status_list = ['in', 'out']
    team_list = ['FL1_RM1','FL1_RM2','FL1_RM3', 'FL1_RM4', 'FL2_RM1']
    d1 = datetime.strptime(f'1/1/2022', '%m/%d/%Y')
    d2 = datetime.strptime(f'08/08/2022', '%m/%d/%Y')
    timestamps = pd.date_range(d1, d2, freq="1min")
    fake_workers = [{'ID':fake.random_number(digits=10),
                  'Temp':np.random.randint(low=35, high=50), 
                  'noted Date':timestamps[x],
                  'Status':np.random.choice(status_list),
                  'room_id':np.random.choice(team_list)} for x in range(len(timestamps))]
        
    return fake_workers

worker_df = pd.DataFrame(make_workers())
worker_df.head()

prints

index ID Temp noted Date Status room_id
0 4338085472 48 2022-01-01 00:00:00 out FL2_RM1
1 6228118896 35 2022-01-01 00:01:00 out FL2_RM1
2 3343476528 43 2022-01-01 00:02:00 in FL1_RM2
3 1990337765 48 2022-01-01 00:03:00 in FL1_RM1
4 8308542801 40 2022-01-01 00:04:00 in FL1_RM1
  • Related