<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to extract year and week number from a columns in a sparkDataFrame? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29443#M21169</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I have the following sparkdataframe :&lt;/P&gt;
&lt;P&gt;sale_id/ created_at&lt;/P&gt;
&lt;P&gt;1 /2016-05-28T05:53:31.042Z&lt;/P&gt;
&lt;P&gt;2 /2016-05-30T12:50:58.184Z&lt;/P&gt;
&lt;P&gt;3/ 2016-05-23T10:22:18.858Z&lt;/P&gt;
&lt;P&gt;4 /2016-05-27T09:20:15.158Z&lt;/P&gt;
&lt;P&gt;5 /2016-05-21T08:30:17.337Z&lt;/P&gt;
&lt;P&gt;6 /2016-05-28T07:41:14.361Z&lt;/P&gt;
&lt;P&gt;i need t add a year-week columns where it contains year and week number of each row in created_at column:&lt;/P&gt;
&lt;P&gt;sale_id/ created_at /year_week&lt;/P&gt;
&lt;P&gt;1 /2016-05-28T05:53:31.042Z /2016-21&lt;/P&gt;
&lt;P&gt;2 /2016-05-30T12:50:58.184Z /2016-22&lt;/P&gt;
&lt;P&gt;3/ 2016-05-23T10:22:18.858Z /2016-21&lt;/P&gt;
&lt;P&gt;4 /2016-05-27T09:20:15.158Z /2016-21&lt;/P&gt;
&lt;P&gt;5 /2016-05-21T08:30:17.337Z /2016-20&lt;/P&gt;
&lt;P&gt;6 /2016-05-28T07:41:14.361Z /2016-21&lt;/P&gt;
&lt;P&gt;Both pyspark pr SparkR or sparkSql are desirable, i have already tried lubridate package but as my columns are S4 i receive the follwing error:&lt;/P&gt;
&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;Error in as.Date.default(head_df$created_at) :&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Error in as.Date.default(head_df$created_at) : 
  do not know how to convert 'head_df$created_at' to class “Date”&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Sep 2016 07:48:29 GMT</pubDate>
    <dc:creator>dshosseinyousef</dc:creator>
    <dc:date>2016-09-20T07:48:29Z</dc:date>
    <item>
      <title>How to extract year and week number from a columns in a sparkDataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29443#M21169</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I have the following sparkdataframe :&lt;/P&gt;
&lt;P&gt;sale_id/ created_at&lt;/P&gt;
&lt;P&gt;1 /2016-05-28T05:53:31.042Z&lt;/P&gt;
&lt;P&gt;2 /2016-05-30T12:50:58.184Z&lt;/P&gt;
&lt;P&gt;3/ 2016-05-23T10:22:18.858Z&lt;/P&gt;
&lt;P&gt;4 /2016-05-27T09:20:15.158Z&lt;/P&gt;
&lt;P&gt;5 /2016-05-21T08:30:17.337Z&lt;/P&gt;
&lt;P&gt;6 /2016-05-28T07:41:14.361Z&lt;/P&gt;
&lt;P&gt;i need t add a year-week columns where it contains year and week number of each row in created_at column:&lt;/P&gt;
&lt;P&gt;sale_id/ created_at /year_week&lt;/P&gt;
&lt;P&gt;1 /2016-05-28T05:53:31.042Z /2016-21&lt;/P&gt;
&lt;P&gt;2 /2016-05-30T12:50:58.184Z /2016-22&lt;/P&gt;
&lt;P&gt;3/ 2016-05-23T10:22:18.858Z /2016-21&lt;/P&gt;
&lt;P&gt;4 /2016-05-27T09:20:15.158Z /2016-21&lt;/P&gt;
&lt;P&gt;5 /2016-05-21T08:30:17.337Z /2016-20&lt;/P&gt;
&lt;P&gt;6 /2016-05-28T07:41:14.361Z /2016-21&lt;/P&gt;
&lt;P&gt;Both pyspark pr SparkR or sparkSql are desirable, i have already tried lubridate package but as my columns are S4 i receive the follwing error:&lt;/P&gt;
&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;Error in as.Date.default(head_df$created_at) :&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Error in as.Date.default(head_df$created_at) : 
  do not know how to convert 'head_df$created_at' to class “Date”&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2016 07:48:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29443#M21169</guid>
      <dc:creator>dshosseinyousef</dc:creator>
      <dc:date>2016-09-20T07:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract year and week number from a columns in a sparkDataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29444#M21170</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;val data = spark.read.option("header","true").option("inferSchema","true").csv("location of file")&lt;/P&gt;
&lt;P&gt;import spark.implicits._&lt;/P&gt;
&lt;P&gt;//creates year column&lt;/P&gt;
&lt;P&gt;val year = data.withColumn("Year",year(data("created_at")))&lt;/P&gt;
&lt;P&gt;//creates weekof year column&lt;/P&gt;
&lt;P&gt;val week = data.withColumn("Week",weekofyear(data("created_at")))&lt;/P&gt;
&lt;P&gt;// concatenate year and week columns&lt;/P&gt;
&lt;P&gt;val new_df = year + "-" +week&lt;/P&gt;
&lt;P&gt;new_df.show()&lt;/P&gt;
&lt;P&gt;//NOTE CODE IN SCALA. I DID NOT TEST THIS CODE ON IDE, BUT IT SHOULD WORK FINE.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Dec 2016 16:36:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29444#M21170</guid>
      <dc:creator>theodondre</dc:creator>
      <dc:date>2016-12-19T16:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract year and week number from a columns in a sparkDataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29445#M21171</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0693f000007OrmsAAC"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2525i17A45400A126CFF9/image-size/large?v=v2&amp;amp;px=999" role="button" title="0693f000007OrmsAAC" alt="0693f000007OrmsAAC" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;THIS IS HOW HE DOCUMENTATION LOOKS LIKE&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Dec 2016 16:45:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-year-and-week-number-from-a-columns-in-a/m-p/29445#M21171</guid>
      <dc:creator>theodondre</dc:creator>
      <dc:date>2016-12-19T16:45:24Z</dc:date>
    </item>
  </channel>
</rss>

