How to extract year and week number in a dataframe and put it in a new column, python
<p>I have the following dataframe :</p>

<pre><code>sale_id      created_at
1               2016-05-28T05:53:31.042Z
2               2016-05-30T12:50:58.184Z
3               2016-05-23T10:22:18.858Z
4               2016-05-27T09:20:15.158Z
5               2016-05-21T08:30:17.337Z
6               2016-05-28T07:41:14.361Z
</code></pre>

<p>i need t add a year-week columns where it contains year and week number of each row in created_at column:</p>

<pre><code>sale_id      created_at                      year_week
1               2016-05-28T05:53:31.042Z       2016-21
2               2016-05-30T12:50:58.184Z       2016-22
3               2016-05-23T10:22:18.858Z       2016-21
4               2016-05-27T09:20:15.158Z       2016-21
5               2016-05-21T08:30:17.337Z       2016-20
6               2016-05-28T07:41:14.361Z       2016-21
</code></pre>

<p>I'd prefer a solution that can be easily transferred to pyspark as well.</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.strftime.html" rel="nofollow"><code>strftime</code></a>:</p>

<p><a href="http://strftime.org/" rel="nofollow">Python's strftime directives</a>.</p>

<pre><code>#if dtype is not datetime
df.created_at = pd.to_datetime(df.created_at)

df['year_week'] = df.created_at.dt.strftime('%Y-%U')
print (df)
   sale_id              created_at year_week
0        1 2016-05-28 05:53:31.042   2016-21
1        2 2016-05-30 12:50:58.184   2016-22
2        3 2016-05-23 10:22:18.858   2016-21
3        4 2016-05-27 09:20:15.158   2016-21
4        5 2016-05-21 08:30:17.337   2016-20
5        6 2016-05-28 07:41:14.361   2016-21
</code></pre>

<p>Another solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.year.html" rel="nofollow"><code>dt.year</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.week.html" rel="nofollow"><code>dt.week</code></a>:</p>

<pre><code>df['year_week'] = df.created_at.dt.year.astype(str) +'-'+  df.created_at.dt.week.astype(str)
print (df)
   sale_id              created_at year_week
0        1 2016-05-28 05:53:31.042   2016-21
1        2 2016-05-30 12:50:58.184   2016-22
2        3 2016-05-23 10:22:18.858   2016-21
3        4 2016-05-27 09:20:15.158   2016-21
4        5 2016-05-21 08:30:17.337   2016-20
5        6 2016-05-28 07:41:14.361   2016-21
</code></pre>

python pandas apache-spark pyspark spark-dataframe