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