Add up the value of data[x] to data[x+1] - python

I have a long list of data which I am working with now,containing a list of 'timestamp' versus 'quantity'. However, the timestamp in the list is not all in order (for example,timestamp[x] can be 140056 while timestamp[x+1] can be 560). I am not going to arrange them, but to add up the value of timestamp[x] to timestamp[x+1] when this happens.
ps:The arrangement of quantity needs to be in the same order as in the list when plotting.
I have been working with this using the following code, which timestamp is the name of the list which contain all the timestamp values:
for t in timestamp:
previous = timestamp[t-1]
increment = 0
if previous > timestamp[t]:
increment = previous
t += increment
delta = datetime.timedelta(0, (t - startTimeStamp) / 1000);
timeAtT = fileStartDate + (delta + startTime)
print("time at t=" + str(t) + " is: " + str(timeAtT));
previous = t
However it comes out with TypeError: list indices must be integers, not tuples. May I know how to solve this, or any other ways of doing this task? Thanks!

The problem is that you're treating t as if it is an index of the list. In your case, t holds the actual values of the list, so constructions like timestamp[t] are not valid. You either want:
for t in range(len(timestamp)):
Or if you want both an index and the value:
for (t, value) in enumerate(timestamp):

When you for the in timestamp you are making t take on the value of each item in timestamp. But then you try to use t as an index to make previous. To do this, try:
for i, t, in enumerate(timestamp):
previous = timestamp[i]
current = t
Also when you get TypeErrors like this make sure you try printing out the intermediate steps, so you can see exactly what is going wrong.

Related

Seperating tuples in list in Python

I want to seperate tuples in list that comes from a sqlite database. But I don't know why I can't seperate them anyway.
Here is the output : [('3:45',), ('4:52',), ('5:42',), ('6:52',)]
I'm pulling that output from sqlite database like this :
asking = "Select SongTimes from Song_List"
self.cursor.execute(asking)
times = list(self.cursor.fetchall())
print(times)
And after that I want to sum all of song times in that list. But I need to acquire them "3:45" like this. After I will seperate them 3,45 like this and the rest is kinda easy. But like I said I need to focus that output for seperating these tuples. That "," is troublemaker I guess.
You can do the following:
times = [('3:45',), ('4:52',), ('5:42',), ('6:52',)]
seconds = 0 # Total number of seconds
# Iterate over all the tuples
for time in times:
time = time[0] # Get first element of tuple
m, s = time.split(":") # split string in two removing ':'
seconds += 60 * int(m) + int(s) # Convert str to int and add to total sum
print(seconds)

Assign value via iloc to Pandas Data Frame

Code snippet:
for row in df.itertuples():
current_index = df.index.get_loc(row.Index)
if current_index < max_index - 29:
if df.iloc[current_index + 30].senkou_span_a == 0.0:
df.iloc[current_index + 30].senkou_span_a = 6700
if df.iloc[current_index + 30].senkou_span_b == 0.0:
df.iloc[current_index + 30].senkou_span_b = 6700.0
the last line where I am assigning a value via iloc, it goes through, but the resultant value is 0.0. I ran into this before where I was assigning the value to a copy of the df, but that doesn't seem to be the case this time. I have been staring at this all day. I hope it is just something silly.
df is timeseries(DateTimeIndex) financial data. I have verified the correct index exists, and no exceptions are thrown.
There is other code to assign values and those work just fine, omitted that code for the sake of brevity.
EDIT
This line works:
df.iloc[current_index + 30, df.columns.get_loc('senkou_span_b')] = 6700
why does this one, and not the original?
I'm not sure exactly what's causing your problem (I'm pretty new to Python), but here's some things that came to mind that might be helpful:
Assuming that the value you're replacing is always 0 or 0.0, maybe try switching from = to += to add instead of assign?
Is your dataframe in tuple format when you attempt to assign the value? Your issue might be that tuples are immutable.
If senkou_span_a refers to a column that you're isolating, maybe try only using iloc to isolate the value like df.iloc[(currentindex + 30), 1] == 0.0
Hope this helped!

python subtracting multiple decimal places

is it possible in python to somehow subtract using multiple decimal places like in version numbers.
for example,
8.0.18 attempting to find a previous version of 8.0.17
any way or method to subtract 1 to get 8.0.17?
i was thinking of regex and pulling out the 18 and subtracting 1 then make myself a variable from the 8.0. and add 17 back to it :), something like this
version_found = "8.0.18"
version = re.search('^\d.\d\d.(\d\d)$', version_found).group(1)
prev_version = int(version) - 1
so prev_version would end up being 17, then i could convert back to a string and take it on to 8.0.
but was wondering if there something method i don't know about or am not considering? thanks
Here is a tiny little script I wrote, it should be fairly easy to implement in your code:
#!/usr/bin/env python3.6
version = "8.0.18"
version = version.split(".")
if int(version[-1]) > 0:
version[-1] = str(int(version[-1]) - 1)
version = '.'.join(version)
print(version)
else:
print("Error, version number ended in a zero!")
This works by splitting the string into a list on each period, resulting in ["8", "0", "18"]. Then it gets the last element in the list by accessing index -1. Then we subtract 1 from the value of that index and assign it back to the same index. Lastly, join the list into a string with periods in between each element then print the outcome.
I think the best way to do this would be count the number of periods in the string and split the text at the specific period you'd like it to subtract at. Then you'd have to turn the string into an integer, subtract 1 from that integer then readd it to the version number.
There are several ways of doing this but thats the way I'd do it. Also keep it in a function so you can call it multiple times at different points of periods.
version = "8.0.18"
index = version.rindex(".") + 1
version = version[:index] + str(int(version[index:])-1)
Just use rindex to find your last period.
Then, convert everything after that to a number, subtract one, turn it back into a string, and you're done.
This becomes more complicated if you want any value other than the last version number. You'd have to rindex from the location that is returned each time. E.g., to change the value after the "second from last" (i.e. first) decimal place, it gets uglier:
start_index = version.rindex(".")
for _ in range(1,1):
end_index = start_index
start_index = version.rindex(".", end=end_index)
version = version[:start_index+1] +
str(int(version[start_index+1:end_index])) +
version[end_index:]
lst = version.split('.') # make a list from individual parts
last_part = lst.pop() # returns the last element, deleting it from the list
last_part = int(last_part) - 1 # converts to an integer and decrements it
last_part = str(last_part) # and converts back to string
lst.append(last_part) # appends it back (now decremented)
version = '.'.join(lst) # convert lst back to string with period as delimiter
Based on Steampunkery
version = "6.4.2"
nums = version.split(".")
skip = 0 # skip from right, e.g. to go directly to 6.3.2, skip=1
for ind in range(skip,len(nums)):
curr_num = nums[-1-ind]
if int(curr_num) > 0:
nums[-1-ind] = str(int(curr_num) - 1)
break
else:
nums[-1-ind] = "x"
oldversion = '.'.join(nums)
print(oldversion)
Sample outputs:
8.2.0 --> 8.1.x
8.2.1 --> 8.2.0
8.0.0 --> 7.x.x
0.0.0 --> x.x.x
8.2.0 --> 8.1.0 (with skip=1)

Function problems

So I have two lists:
typeList — list of strings of transaction types (addition, subtraction)
valueList — List of values either added or subtracted.
as both value and type are appended to the list at the same index, I need to make a function to calculate the sum of the total value added and the total value subtracted, but I've been really stuck on it for ages trying to get my head around just how to do it.
desiredType is just the type of transaction that is being looked for. As you'll see, I called my function twice with each type.
I understand that the index values need to be obtained and used between the two lists but not sure how to sum the values up.
def showInfo(typeList, valueList, desiredType):
for i in range(len(valueList)):
if typeList[i] == desiredType:
total = total + valueList[i]
return (total)
Call to the function:
if choice == "I": #Prints transaction information
print (showInfo(transactionTypes,transactionAmounts, "Addition"))
print (showInfo(transactionTypes,transactionAmounts, "Subtraction"))
Any help would be appreciated as well as an explanation on how it's done.
You can zip together those 2 lists, then you don't have to keep track of the index you're looking at:
def transaction_total(types, amounts, desired_type):
total = 0
for transaction_type, amount in zip(types, amounts):
if transaction_type == desired_type:
total += amount
return total
However, you're really just filtering values and summing them. Python makes this cleaner using generator expressions and the sum function:
def transaction_total(types, amounts, desired_type):
return sum(amount for transaction_type, amount in zip(types, amounts)
if transaction_type == desired_type)
If you need to keep your data unmodified, you can just make your function more efficient (just one call) :
def showInfo(typeList, valueList):
for i in range(len(valueList)):
if typeList[i] == "Addition":
total_add = total_add + valueList[i]
else:
total_sub = total_sub - valueList[i]
return (total_add, total_sub)

ValueError with recursive function in Python

def recursive(start, end, datelist):
results = ga.GAnalytics().create_query(profile_id,
metrics,
start,
end,
dimensions).execute()
if results.get("containsSampledData") is True:
x = len(datelist) / 2
recursive(datelist[0],datelist[:x][-1],datelist[:x])
recursive(datelist[x:][0],datelist[-1],datelist[x:])
else:
unsampled_date_ranges = []
for x, y in start, end:
unsampled_date_ranges.append((x, y))
recursive(start_date, end_date, date_list)
The function above takes a start date, end date and an inclusive list of dates based on the start and end dates. If first checks if the data returned for the initial date range is sampled, if it is then the date range is split in half then checked, and so on.
My issue is with the else statement. To make sure the function worked I tried print start + " - " + end which returned the expected date ranges. Ideally, I would like the data to be returned as a list of tuples, so I tried the above, but unfortunately I am getting this error ValueError: too many values to unpack here for x, y in start, end:
What is the issue with my code in my else statement and how can I get it to return a list of tuples?

Categories

Resources