<?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 Re: How do I cast using a DataFrame? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30639#M22233</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I am trying to store a dataframe as table in databricks and encountering the following error, can someone help?&lt;/P&gt;
&lt;P&gt;"typeerror: field date: can not merge type &amp;lt;class 'pyspark.sql.types.stringtype'&amp;gt; and &amp;lt;class 'pyspark.sql.types.doubletype'&amp;gt;"&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Mar 2020 14:24:38 GMT</pubDate>
    <dc:creator>srisre111</dc:creator>
    <dc:date>2020-03-19T14:24:38Z</dc:date>
    <item>
      <title>How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30634#M22228</link>
      <description />
      <pubDate>Tue, 28 Apr 2015 20:03:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30634#M22228</guid>
      <dc:creator>cfregly</dc:creator>
      <dc:date>2015-04-28T20:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30635#M22229</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use HiveQL's cast() type conversion function to cast an element of a nested map in Python as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql import Row 
df = sqlContext.createDataFrame([Row(a={'b': 1})])
str = df.selectExpr("cast(a['b'] AS STRING)")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;or in Scala as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;val df = Seq((Map("a" -&amp;gt; 1))).toDF("a") 
df.selectExpr("cast(a['a'] AS STRING)")&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Apr 2015 20:06:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30635#M22229</guid>
      <dc:creator>cfregly</dc:creator>
      <dc:date>2015-04-28T20:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30636#M22230</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;If your df is registered as a table you can also do this with a SQL call:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;df.createOrReplaceTempView("table")
str = spark.sql('''
    SELECT CAST(a['b'] AS STRING)
    FROM table
''')
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Its more code in the simple case but I have found in the past that when this is combined into a much more complex query the SQL format can be more friendly from a readability standpoint.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 15:57:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30636#M22230</guid>
      <dc:creator>Grr</dc:creator>
      <dc:date>2017-02-01T15:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30637#M22231</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Could also use withColumn() to do it without Spark-SQL, although the performance will likely be different. The question being, would creating a new column take more time than using Spark-SQL.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;val dfNew = df.withColumn("newColName", df.originalColName.cast(IntegerType))
    .drop("originalColName").withColumnRenamed("newColName", "originalColName")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Create the new column, casting from the original column, drop the original, then rename the new column back to the original name. A bit roundabout, but looks like it could work.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2017 19:26:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30637#M22231</guid>
      <dc:creator>DarrellUlm</dc:creator>
      <dc:date>2017-03-15T19:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30638#M22232</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Is it safe to cast a column that contains null values?&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 04:33:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30638#M22232</guid>
      <dc:creator>ShubhamGupta187</dc:creator>
      <dc:date>2018-04-20T04:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: How do I cast using a DataFrame?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30639#M22233</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I am trying to store a dataframe as table in databricks and encountering the following error, can someone help?&lt;/P&gt;
&lt;P&gt;"typeerror: field date: can not merge type &amp;lt;class 'pyspark.sql.types.stringtype'&amp;gt; and &amp;lt;class 'pyspark.sql.types.doubletype'&amp;gt;"&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 14:24:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-cast-using-a-dataframe/m-p/30639#M22233</guid>
      <dc:creator>srisre111</dc:creator>
      <dc:date>2020-03-19T14:24:38Z</dc:date>
    </item>
  </channel>
</rss>

