I’m trying to run this on my own but when I run your script it fails, I also saved the sales_times.csv.
# from matplotlib import pyplot as plt
import csv
def convert_time_to_num(time):
mins = int(time[-2:])
frac_of_hour = mins / 60.0
hour = int(time[:-3])
time = hour + frac_of_hour
return time
sales_times_raw = []
with open("sales_times.csv") as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
sales_times_raw.append(row[2])
sales_times_raw = sales_times_raw[1:]
sales_times = []
for time in sales_times_raw:
sales_times.append(convert_time_to_num(time))
When I run this it spits out
Traceback (most recent call last):
File "script.py", line 17, in <module>
sales_times_raw.append(row[2])
IndexError: list index out of range
Alternatively, I used Pandas to be able to get the list as such.
# import csv
import pandas as pd
sales = pd.read_csv("sales_times.csv")
def convert_time_to_num(time):
mins = int(time[-2:])
frac_of_hour = mins / 60.0
hour = int(time[:-3])
time = hour + frac_of_hour
return time
# sales_times_raw = []
sales_times_raw = list(sales.time)
# with open("sales_times.csv") as csvDataFile:
# csvReader = csv.reader(csvDataFile)
# for row in csvReader:
# sales_times_raw.append(row[])
# sales_times_raw = sales_times_raw[1:]
sales_times = []
for time in sales_times_raw:
sales_times.append(convert_time_to_num(time))
Going through this exercise and curious at some of the lines written in the script.py file, specifically, lines 4 - 9:
def convert_time_to_num(time):
mins = int(time[-2:])
frac_of_hour = mins/60.0
hour = int(time[:-3])
time = hour + frac_of_hour
return time
I’m curious as to how this function works. Maybe this is a result of me overthinking, as I’ve been staring at this for the last 30 min, but I can’t seem to figure out how this function converts time to numbers.
I’m not sure if I have the best grasp on slice notation, so any help on this would be appreciated.
To my understanding, the first command mins = int(time[-2:]) would mean anything after the second to last index (with the second to last digit included). Is this correct? For example, if the time were 12:30, “min” would have a value of 30. Another possibility could be 120 minutes, and in this case mean would have a value of 20.
Following that same logic, hour = int(time[:-3]) doesn’t make sense to me. Doesn’t this mean that everything except the last two items? So 12:30 would have a value of 1? Or 120 would have a value of 0?
I simply don’t understand the formula and can’t seem to grasp it.
This goes without saying, but I also don’t understand how adding hour + frac_of_hour calculates the time.
As can be seen in the file sales_times.csv, times arrive in format like “21:03” or “9:37”.
So suppose time = "21:03"
time[-2:] means the last 2 digits i.e. “03”.
So int(time[-2:]) will equal 3
time[:-3] means time except the last 3 digits, i.e. “21”
So int(time[:-3]) will equal 21
In the original code they use the variable time for both input and output, which is confusing. I will use a different variable name time_n for the output. So
time_n = 21 + 3/60.0 = 21.05
Which is just the decimal equivalent of the time duration 21:03