The variable "data" in the code below contains hundreds of execution results from querying a database. Each execution result is one day of data containing roughly 7,000 rows of data (columns are timestamp, and value). I append each day to each other resulting in several million rows of data (these hundreds of appends take a long time). After I have the complete data set for one sensor I store this data as a column in the unitdf DataFrame, I then repeat the above process for each sensor and merge them all into the unitdf DataFrame.
Both the append and merge are costly operations I believe. The only possible solution I may have found is splitting up each column into lists and once all data is added to the list bring all the columns together into a DataFrame. Any suggestions to speed things up?
i = 0
for sensor_id in sensors: #loop through each of the 20 sensors
#prepared statement to query Cassandra
session_data = session.prepare("select timestamp, value from measurements_by_sensor where unit_id = ? and sensor_id = ? and date = ? ORDER BY timestamp ASC")
#Executing prepared statement over a range of dates
data = execute_concurrent(session, ((session_data, (unit_id, sensor_id, date)) for date in dates), concurrency=150, raise_on_first_error=False)
sensordf = pd.DataFrame()
#Loops through the execution results and appends all successful executions that contain data
for (success, result) in data:
if success:
sensordf = sensordf.append(pd.DataFrame(result.current_rows))
sensordf.rename(columns={'value':sensor_id}, inplace=True)
sensordf['timestamp'] = pd.to_datetime(sensordf['timestamp'], format = "%Y-%m-%d %H:%M:%S", errors='coerce')
if i == 0:
i+=1
unitdf = sensordf
else:
unitdf = unitdf.merge(sensordf, how='outer')