I have the following code which converts index to week number as well as another column is added with week number:
JavaScript
x
62
62
1
import yfinance as yf
2
import pandas as pd
3
4
df = yf.download('f', interval = '1wk')
5
df['week'] = df.index.isocalendar().week
6
df.index = pd.to_datetime(df.index)
7
df.index = df.index.strftime('%U')
8
df = df[df['week'] ==12]
9
print(df)
10
Open High Low Close Adj Close Volume week
11
Date
12
11 2.092394 2.092394 1.950468 2.011294 0.262594 7956172.0 12
13
11 1.682837 1.690947 1.646341 1.654451 0.229500 8985757.0 12
14
11 1.192178 1.248949 1.184068 1.224618 0.184168 10983278.0 12
15
11 1.820708 1.849093 1.784212 1.800432 0.286859 16121960.0 12
16
12 1.901808 1.909918 1.804487 1.808542 0.303502 11485741.0 12
17
12 1.809556 1.829831 1.794350 1.809556 0.326586 11250230.0 12
18
11 1.758868 1.784212 1.758868 1.769006 0.346003 13469697.0 12
19
11 1.196233 1.211440 1.135408 1.145545 0.248064 21795165.0 12
20
11 0.907313 1.054307 0.902244 1.013757 0.237924 33698903.0 12
21
12 0.846487 0.876900 0.821143 0.841418 0.206226 25282194.0 12
22
12 1.642286 1.692974 1.596667 1.616942 0.396302 32589171.0 12
23
12 2.356985 2.364588 2.197318 2.258144 0.563657 50150085.0 12
24
11 2.623096 2.661112 2.569874 2.592684 0.680899 70440945.0 12
25
11 4.409843 4.964875 4.394637 4.820415 1.337498 77535017.0 12
26
11 7.276241 7.470122 7.093765 7.424503 2.144691 40895183.0 12
27
12 8.097384 8.120194 7.664003 7.664003 2.311799 25503044.0 12
28
12 9.055384 9.101003 8.781670 8.850099 2.806630 22920790.0 12
29
11 8.827289 8.918527 8.667622 8.804480 2.974335 23601426.0 12
30
11 6.295431 6.432288 5.862050 5.976097 2.199914 26059828.0 12
31
11 6.637574 7.413098 6.637574 7.344669 2.845779 78490758.0 12
32
12 9.420337 9.762480 9.420337 9.557194 3.842265 39688451.0 12
33
12 11.245099 11.701290 10.948576 10.994195 4.545142 56170594.0 12
34
12 9.351908 9.397527 9.032575 9.306289 3.979715 57608865.0 12
35
11 11.997814 12.454005 11.997814 12.271528 5.494052 41265643.0 12
36
11 11.587242 11.860957 11.496004 11.587242 5.431107 25078605.0 12
37
11 21.897152 23.288532 21.805914 23.220104 11.327546 32670825.0 12
38
12 32.182701 32.972672 30.362335 31.289692 15.777828 22828360.0 12
39
12 24.214306 25.244701 23.493029 24.523424 12.810479 44824758.0 12
40
11 28.400000 30.090000 27.020000 27.900000 15.203670 28355400.0 12
41
11 17.000000 17.270000 16.299999 16.670000 9.438626 36153400.0 12
42
11 6.700000 8.030000 6.600000 8.020000 4.709421 89985100.0 12
43
11 13.270000 13.440000 13.000000 13.060000 7.929460 39367600.0 12
44
12 11.310000 11.630000 10.940000 11.290000 7.051045 58470600.0 12
45
12 7.850000 8.250000 7.810000 8.090000 5.279247 93573100.0 12
46
11 7.580000 8.230000 7.570000 7.890000 5.259418 201311700.0 12
47
11 5.020000 5.730000 4.950000 5.620000 3.746252 393914200.0 12
48
11 2.280000 2.780000 2.090000 2.750000 1.833130 298598800.0 12
49
12 12.980000 14.300000 12.810000 13.860000 9.238977 578653600.0 12
50
12 14.700000 15.200000 14.020000 15.010000 10.005558 342805300.0 12
51
12 12.520000 12.680000 12.180000 12.320000 8.244656 215053600.0 12
52
11 13.160000 13.420000 12.800000 13.260000 9.070373 158372200.0 12
53
11 15.180000 15.740000 15.160000 15.470000 10.870311 132509200.0 12
54
11 16.260000 16.540001 16.110001 16.480000 11.979823 152515700.0 12
55
12 13.640000 13.760000 12.780000 13.060000 10.109502 114791900.0 12
56
12 12.480000 12.490000 11.500000 11.620000 9.458299 332979200.0 12
57
11 11.150000 11.190000 10.510000 10.560000 9.152753 238518100.0 12
58
11 8.450000 8.870000 8.420000 8.540000 7.878255 237838700.0 12
59
11 5.040000 5.220000 4.100000 4.330000 4.257940 596481500.0 12
60
12 12.850000 12.930000 11.720000 12.300000 12.095303 312358300.0 12
61
12 16.870001 17.309999 16.330000 16.469999 16.361143 331864800.0 12
62
As you can see there are instances where week 11 is being shown in the index column using df.index.strftime('%U')
when the week number is showing as 12 using df.index.isocalendar().week
Why are there week number different in index column as compare to column week
?
Edit
With this:
JavaScript
1
9
1
df = yf.download('f', interval = '1wk')
2
print(len(df.index.year))
3
df['week'] = df.index.isocalendar().week
4
5
df.index = pd.to_datetime(df.index)
6
df['dates_with_strf'] = df.index.strftime('%U')
7
df = (df[df['week'] ==12])
8
print(df)
9
I have dates
JavaScript
1
53
53
1
Open High Low Volume week dates_with_strf
2
Date
3
1973-03-19 2.092394 2.092394 1.950468 7956172.0 12 11
4
1974-03-18 1.682837 1.690947 1.646341 8985757.0 12 11
5
1975-03-17 1.192178 1.248949 1.184068 10983278.0 12 11
6
1976-03-15 1.820708 1.849093 1.784212 16121960.0 12 11
7
1977-03-21 1.901808 1.909918 1.804487 11485741.0 12 12
8
1978-03-20 1.809556 1.829831 1.794350 11250230.0 12 12
9
1979-03-19 1.758868 1.784212 1.758868 13469697.0 12 11
10
1980-03-17 1.196233 1.211440 1.135408 21795165.0 12 11
11
1981-03-16 0.907313 1.054307 0.902244 33698903.0 12 11
12
1982-03-22 0.846487 0.876900 0.821143 25282194.0 12 12
13
1983-03-21 1.642286 1.692974 1.596667 32589171.0 12 12
14
1984-03-19 2.356985 2.364588 2.197318 50150085.0 12 12
15
1985-03-18 2.623096 2.661112 2.569874 70440945.0 12 11
16
1986-03-17 4.409843 4.964875 4.394637 77535017.0 12 11
17
1987-03-16 7.276241 7.470122 7.093765 40895183.0 12 11
18
1988-03-21 8.097384 8.120194 7.664003 25503044.0 12 12
19
1989-03-20 9.055384 9.101003 8.781670 22920790.0 12 12
20
1990-03-19 8.827289 8.918527 8.667622 23601426.0 12 11
21
1991-03-18 6.295431 6.432288 5.862050 26059828.0 12 11
22
1992-03-16 6.637574 7.413098 6.637574 78490758.0 12 11
23
1993-03-22 9.420337 9.762480 9.420337 39688451.0 12 12
24
1994-03-21 11.245099 11.701290 10.948576 56170594.0 12 12
25
1995-03-20 9.351908 9.397527 9.032575 57608865.0 12 12
26
1996-03-18 11.997814 12.454005 11.997814 41265643.0 12 11
27
1997-03-17 11.587242 11.860957 11.496004 25078605.0 12 11
28
1998-03-16 21.897152 23.288532 21.805914 32670825.0 12 11
29
1999-03-22 32.182701 32.972672 30.362335 22828360.0 12 12
30
2000-03-20 24.214306 25.244701 23.493029 44824758.0 12 12
31
2001-03-19 28.400000 30.090000 27.020000 28355400.0 12 11
32
2002-03-18 17.000000 17.270000 16.299999 36153400.0 12 11
33
2003-03-17 6.700000 8.030000 6.600000 89985100.0 12 11
34
2004-03-15 13.270000 13.440000 13.000000 39367600.0 12 11
35
2005-03-21 11.310000 11.630000 10.940000 58470600.0 12 12
36
2006-03-20 7.850000 8.250000 7.810000 93573100.0 12 12
37
2007-03-19 7.580000 8.230000 7.570000 201311700.0 12 11
38
2008-03-17 5.020000 5.730000 4.950000 393914200.0 12 11
39
2009-03-16 2.280000 2.780000 2.090000 298598800.0 12 11
40
2010-03-22 12.980000 14.300000 12.810000 578653600.0 12 12
41
2011-03-21 14.700000 15.200000 14.020000 342805300.0 12 12
42
2012-03-19 12.520000 12.680000 12.180000 215053600.0 12 12
43
2013-03-18 13.160000 13.420000 12.800000 158372200.0 12 11
44
2014-03-17 15.180000 15.740000 15.160000 132509200.0 12 11
45
2015-03-16 16.260000 16.540001 16.110001 152515700.0 12 11
46
2016-03-21 13.640000 13.760000 12.780000 114791900.0 12 12
47
2017-03-20 12.480000 12.490000 11.500000 332979200.0 12 12
48
2018-03-19 11.150000 11.190000 10.510000 238518100.0 12 11
49
2019-03-18 8.450000 8.870000 8.420000 237838700.0 12 11
50
2020-03-16 5.040000 5.220000 4.100000 596481500.0 12 11
51
2021-03-22 12.850000 12.930000 11.720000 312358300.0 12 12
52
2022-03-21 16.870001 17.309999 16.330000 331864800.0 12 12
53
Advertisement
Answer
data
JavaScript
1
7
1
import pandas.util.testing
2
df= pd.util.testing.makeMixedDataFrame().set_index('D').rename_axis(None)
3
4
df['week_strf'] =df.index.strftime('%U')
5
6
df['week_iso'] = df.index.isocalendar().week
7
Check documentation
stftime, Sunday as the first day of the week
ISO weeks start on a Monday and end on a Sunday
JavaScript
1
7
1
A B C week_strf week_iso
2
2009-01-01 0.0 0.0 foo1 00 1
3
2009-01-02 1.0 1.0 foo2 00 1
4
2009-01-05 2.0 0.0 foo3 01 2
5
2009-01-06 3.0 1.0 foo4 01 2
6
2009-01-07 4.0 0.0 foo5 01 2
7