list index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: list index out of range (/thread-18623.html) |
list index out of range - mcgrim - May-24-2019 I am converting a list of times and dates taken from a file into european time. The conversion function seems to do its job, but when I go and print the outcome, i can only print the first value. The list of original times is over 200000 values but with this new conversion, only the first value is the one that shows. In order for what I am saying to make sense, kindly refer to my code and its comments. If the info I am giving are not clear/enough, please say something. How do I fix this and make sure that this function has as many values as the original list of dates? from astral import Astral from scipy import * from pylab import* import numpy as np from numpy import array import matplotlib.pyplot as plt import datetime from datetime import timezone from datetime import timedelta import matplotlib.dates as dates import pandas as pd import pytz import sys def last_digits(num, last_digits_count=2): return abs(num) % (10**last_digits_count) orig_date=[] orig_time=[] movements=[] with open('bird_jan25jan16.txt', 'r') as f: for line in f: data = line.split() # Splits on whitespace orig_date.append(data[0][:]) orig_time.append((data[1][:])) movements.append(int(data[2][:])) for i in range(0,len(orig_date)): if ((len(str(movements[i-1])) - len(str(movements[1]))) >=2): if movements[i]==0 or ( (movements[i-1] == movements[i+1] ) and (last_digits(movements[i-1]) == last_digits(movements[i]))): movements[i]=((movements[i-1]+movements[i+1])/2) """ putting date and time together and converting them to datetime objects""" dt_fmt = '%Y-%m-%d %H:%M:%S.%f timestamps = [] for col_dt in zip(orig_date , orig_time): new_dt_str = ' '.join(col_dt) new_dt = datetime.datetime.strptime(new_dt_str, dt_fmt) timestamps.append(new_dt) def convert_local_timezone(): #function for conversion converted_dates=[] for date in timestamps: local_tz = pytz.timezone('Europe/Berlin') local_time = date.replace(tzinfo=pytz.utc).astimezone(local_tz) converted_dates.append(local_time) return converted_dates CEU_times=convert_local_timezone() print(timestamps[0]) #works print(CEU_times[0]) #works print(CEU_times[1]) # gives the error print(len(timestamps)) #238748 print(len(CEU_times)) # should be 238748 too but instead is 1 #print(CEU_times[0].year)
I hope to get some help. Again, if additional info are needed, don't hesitate to ask. RE: list index out of range - SheeppOSU - May-24-2019 Try this. Fix is in "convert_local_timezone" from astral import Astral from scipy import * from pylab import* import numpy as np from numpy import array import matplotlib.pyplot as plt import datetime from datetime import timezone from datetime import timedelta import matplotlib.dates as dates import pandas as pd import pytz import sys def last_digits(num, last_digits_count=2): return abs(num) % (10**last_digits_count) orig_date=[] orig_time=[] movements=[] with open('bird_jan25jan16.txt', 'r') as f: for line in f: data = line.split() # Splits on whitespace orig_date.append(data[0][:]) orig_time.append((data[1][:])) movements.append(int(data[2][:])) for i in range(0,len(orig_date)): if ((len(str(movements[i-1])) - len(str(movements[1]))) >=2): if movements[i]==0 or ( (movements[i-1] == movements[i+1] ) and (last_digits(movements[i-1]) == last_digits(movements[i]))): movements[i]=((movements[i-1]+movements[i+1])/2) """ putting date and time together and converting them to datetime objects""" dt_fmt = '%Y-%m-%d %H:%M:%S.%f timestamps = [] for col_dt in zip(orig_date , orig_time): new_dt_str = ' '.join(col_dt) new_dt = datetime.datetime.strptime(new_dt_str, dt_fmt) timestamps.append(new_dt) def convert_local_timezone(): #function for conversion converted_dates=[] for date in timestamps: local_tz = pytz.timezone('Europe/Berlin') local_time = date.replace(tzinfo=pytz.utc).astimezone(local_tz) converted_dates.append(local_time) return converted_dates CEU_times=convert_local_timezone() print(timestamps[0]) #works print(CEU_times[0]) #works print(CEU_times[1]) # gives the error print(len(timestamps)) #238748 print(len(CEU_times)) # should be 238748 too but instead is 1 #print(CEU_times[0].year) RE: list index out of range - mcgrim - May-25-2019 thanks |