Phython

Create Random Data with Date Field in Python

First we have to import necessary modules

import pandas as pd
import csv
from random import randint

Now we will create data for one month. we can create data directly by using date_range method of Pandas. This will create data with 1 Day interval as default. However, we can specify 2D for 2 Days, 3D for 3 Days and similarly. We can use different freq values.
Some can be found here

Alias Description
B business day frequency
C custom business day frequency (experimental)
D calendar day frequency
W weekly frequency
M month end frequency
SM semi-month end frequency (15th and end of month)
BM business month end frequency
CBM custom business month end frequency
MS month start frequency
SMS semi-month start frequency (1st and 15th)
BMS business month start frequency
CBMS custom business month start frequency
Q quarter end frequency
BQ business quarter endfrequency
QS quarter start frequency
BQS business quarter start frequency
A year end frequency
BA business year end frequency
AS year start frequency
BAS business year start frequency
BH business hour frequency
H hourly frequency
T, min minutely frequency
S secondly frequency
L, ms milliseconds
U, us microseconds
N nanoseconds

Lets now create for One Month, here i am choosing hourly increment in data generation. You can use the above list for choosing your desired one. Also, as already specified above,in below example you can add freq=’2H’ for 2 Hourly data.

# Creating data from 1st Jan 2016 - 720 hours {frequency}
dateRng = pd.DataFrame(pd.date_range('1/1/2016', periods=720, freq='H'))

Now, since we have created the date fields. Lets create CSV file and store in the folder from where we are executing the script.

# Creating File as GeneratedTickets
f = open("GeneratedTickets.csv", 'wt')

File is opened in background and ready to be updated.
we will add columns and generate data into it.

try:
writer = csv.writer(f)
writer.writerow(('TicketNumber','Priority', 'TicketTime'))
# Creating each Line
for index, row in dateRng.iterrows():
writer.writerow(('TICKET00'+ str(index), 'Priority-'+str(randint(1,5)),row.loc[0]))
finally:
f.close()

Complete Script Body:

# Importing Libs
import pandas as pd
import csv
from random import randint

# Creating data from 1st Jan 2016 - 720 hours {frequency}
dateRng = pd.DataFrame(pd.date_range('1/1/2016', periods=720, freq='H'))

# Creating File as GeneratedTickets
f = open("GeneratedTickets.csv", 'wt')
try:
    writer = csv.writer(f)
    writer.writerow(('TicketID','Priority', 'TicketTime'))
    # Creating each Line
    for index, row in dateRng.iterrows():
       writer.writerow(('TICKET00'+ str(index), 'Priority-'+str(randint(1,5)),row.loc[0]))
finally:
    f.close()
Advertisements

One thought on “Create Random Data with Date Field in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s