Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we change datatype of column in mleap dataframe? #651

Closed
mtsol opened this issue Mar 5, 2020 · 2 comments
Closed

How can we change datatype of column in mleap dataframe? #651

mtsol opened this issue Mar 5, 2020 · 2 comments

Comments

@mtsol
Copy link

mtsol commented Mar 5, 2020

No description provided.

@ancasarb
Copy link
Member

ancasarb commented Mar 9, 2020

There are a few operations in FrameBuilder that you can use to do this.

Suppose you have this leapframe

  val fields = Seq(StructField("test_string", ScalarType.String),
    StructField("test_double", ScalarType.Double))
  val schema: StructType = StructType(fields).get
  val dataset = Seq(
    Row("hello", 42.13),
    Row("there", 13.42)
  )
  val frame = DefaultLeapFrame(schema, dataset)

To change the field type of test_double from Double to String (as an example), you can do

val updatedFrame = frame.withColumn("double_string", "test_double") {
          (r: Double) => r.toString
        }.get
          .drop("test_double").get
updatedFrame.printSchema()

Just to make you aware, mleap can do quite a bit of casting, if that's what you're after

https://github.com/combust/mleap/blob/master/mleap-core/src/main/scala/ml/combust/mleap/core/types/Casting.scala

https://github.com/combust/mleap/blob/master/mleap-core/src/test/scala/ml/combust/mleap/core/types/CastingSpec.scala

Hope this helps!

@ancasarb
Copy link
Member

ancasarb commented May 7, 2020

@mtsol I believe the solution above will achieve what you were looking for, to change the datatype of a column, please re-open the issue if it's not the case. Thank you!

@ancasarb ancasarb closed this as completed May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants