title: Pandas Series.dt.week vs. pd.Period.strftime - what's the difference?
body: <p>When operating on a pandas series of dates, isolating the week number can be performed in two separate ways that produce different results. </p>
  
  <p>Using the <code>.dt.week</code> accessor on a numpy.datetime64 value or a pd.Period within a series produces different results than using <code>pd.Period.strftime</code> on the same objects. The online documentation for <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Period.strftime.html" rel="nofollow noreferrer">pd.Period.strftime</a> states that all days before the first occurrence of the start week in the beginning of the year are counted as week 0. This follows standard python strftime behavior.</p>
  
  <p>The <code>.dt.week</code> accessor seems to start at 1 and restart after 52 weeks, making the final two days of 2018 week 1 of 2019. The online documentation for <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.week.html" rel="nofollow noreferrer">pd.Series.dt.week</a> only states that it returns the week ordinal of the year. This seems to be the iso week number?</p>
  
  <p>Why is there this discrepancy in the behavior of the two methods? Which one should be used and why? How can I elegantly get the iso week number from a single python datetime (or pd.Period or pd.timestamp) object (as opposed to a series)?</p>
  
  <pre><code>df2 = pd.DataFrame({"Date_string": ["2018-12-27", "2018-12-28","2018-12-29", "2018-12-30", "2018-12-31", "2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06", "2019-01-07",]})
  df2["Date_datestamp"] = pd.to_datetime(df2["Date_string"], format='%Y-%m-%d')
  df2["Date_period"] = df2['Date_datestamp'].dt.to_period("D")
  df2["Week1"] = df2['Date_period'].apply(lambda x: (x + timedelta(days=1)).week)
  df2["Week2"] = df2['Date_period'].apply(lambda x: x.strftime("%U"))
  df2 
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>   Date_string  Date_datestamp Date_period  Week1 Week2
  0   2018-12-27     2018-12-27  2018-12-27     52    51
  1   2018-12-28     2018-12-28  2018-12-28     52    51
  2   2018-12-29     2018-12-29  2018-12-29     52    51
  3   2018-12-30     2018-12-30  2018-12-30      1    52
  4   2018-12-31     2018-12-31  2018-12-31      1    52
  5   2019-01-01     2019-01-01  2019-01-01      1    00
  6   2019-01-02     2019-01-02  2019-01-02      1    00
  7   2019-01-03     2019-01-03  2019-01-03      1    00
  8   2019-01-04     2019-01-04  2019-01-04      1    00
  9   2019-01-05     2019-01-05  2019-01-05      1    00
  10  2019-01-06     2019-01-06  2019-01-06      2    01
  11  2019-01-07     2019-01-07  2019-01-07      2    01
  </code></pre>