Search code examples
pythonpandasdataframetype-conversion

How do I convert a Float column in an OML DataFrame into an Integer column?


I am using the oml4py python library, and have created an OML DataFrame containing a column of Floats that I would like to cast as a new column of Integers.

I tried the following:

import pandas as pd
import oml
data = {"id" : [1, 2, 3, 4, 5], "val" : [4.0, 3.0, 6.0, 7.0, 5.0]}
df = pd.DataFrame(data)
DF = oml.push(df)
DF["val"].astype(int)

but that errors out with

AttributeError: 'Float' object has no attribute 'astype'


Solution

  • Here is an example:

    # Import the OML4py 'oml' library and and pandas library

    import oml 
    import pandas as pd
    

    # Create a test DataFrame

    df = pd.DataFrame({"id":[1, 2, 3, 4, 5], "num":[4, 3, 6.3, 7.4, 5]})
    

    # Create temporary OML proxy object DF

    DF = oml.push(df)
    

    # id is class oml.core.float.Float

    type(DF['id']) 
    

    # Convert id to Integer type using the oml.Integer() function

    DF2 = oml.Integer(DF["id"]).concat(DF["num"]) 
    

    # id is now class oml.core.integer.Integer

    type(DF2['id'])