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?
I hope to get some help.
Again, if additional info are needed, don't hesitate to ask.
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)
Error:File "<ipython-input-275-389e619abf53>", line 1, in <module>
runfile('C:/Users/Desktop/python ode/birds_2nd_draft.py', wdir='C:/Users/Desktop/python ode')
File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Desktop/python ode/birds_2nd_draft.py", line 94, in <module>
print(CEU_times[1]) # gives the error
IndexError: list index out of range
I hope to get some help.
Again, if additional info are needed, don't hesitate to ask.