I am building a tool to help automate reviewing of data from several laboratory setups on a weekly basis. A tab-delimited text file is generated each day. Each row represents data taken every 2 seconds so there are 43200 rows and many columns (each file is 75mb)
I am loading the seven text files using pandas.readcsv and only extracting the three columns I need into a pandas dataframe. This is slower than I'd like but acceptable. Then, I plot the data using Plotly offline to view the interactive plot. This is a scheduled task set up to run once a week.
The data is plotted vs date & time. Often times the test setups are offline temporarily and there will be gaps in the data. Unfortunately when this is plotted all the data is connected by lines, even if the test was offline for a period of hours or days.
The only way to prevent this is to insert a line with a date between the two dates with actual data and NaNs for all the missing data. I have implemented this for a missing data file easily enough however I want to generalize this for any gaps in data greater than a certain time period. I came up with a solution that seems to work but it is REALLY slow:
# alldata is a pandas dataframe with 302,000 rows and 4 columns
# one datetime column and three float32 columns
alldata_gaps = pandas.DataFrame() #new dataframe with gaps in it
#iterate over all rows. If the datetime difference between
#two consecutive rows is more than one minute, insert a gap row.
for i in range(0, len(alldata)):
alldata_gaps = alldata_gaps.append(alldata.iloc[i])
if alldata.iloc[i+1, 0]-alldata.iloc[i,0] > datetime.timedelta(minutes=1):
Series = pandas.Series({'datetime' : alldata.iloc[i,0]
+datetime.timedelta(seconds=3)})
alldata_gaps = alldata_gaps.append(Series)
print(Series)
Does anyone have a suggestion how I could speed this operation up so it doesn't take such an obnoxiously long time?
Here's a dropbox link to an example data file with only 100 lines
Here's a link to my current script without adding the gap rows
