Reputation: 79
I try to store the number of weekofyear from the list of data named date_range
which stored the data such as
date_range[1] = DatetimeIndex(['2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05',
'2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09',
'2020-03-10', '2020-03-11', '2020-03-12', '2020-03-13',
'2020-03-14', '2020-03-15', '2020-03-16', '2020-03-17',
'2020-03-18', '2020-03-19', '2020-03-20', '2020-03-21',
'2020-03-22', '2020-03-23', '2020-03-24', '2020-03-25',
'2020-03-26', '2020-03-27', '2020-03-28', '2020-03-29',
'2020-03-30', '2020-03-31', '2020-04-01', '2020-04-02',
'2020-04-03', '2020-04-04', '2020-04-05'],
dtype='datetime64[ns]', freq='D')
by using the following code
weeknumber= []
for i in range(28):
for j in range(35):
weeknumber[i][j] = pd.Timestamp(date_range[i][j]).weekofyear
However,the IDE show the error 'list assignment index out of range'. How to solve this problem ?
Upvotes: 2
Views: 6810
Reputation: 3722
Use this:
weeknumber = []
for i in range(28):
weeknumber.append([])
for j in range(35)):
weeknumber[i].append(pd.Timestamp(date_range[i][j]).weekofyear)
For an explanation of why you get surprising results to your other approach, refer to this answer to your other question
Upvotes: 1
Reputation: 551
When your list
is empty in python
you can not assign value to unassigned index of list.
so you have 2 options here:
append
like : list.append(value)
loop
to assign a value to your list
before your main for
. Like below:i = 0
while ( i < index_you_want):
list[i] = 0
...
#here your main code
Upvotes: 1