Search code examples
pythonpandastypespytorch

Fail to convert float 64 to float 32 in python


I tried to convert the data from data type float 64 to float 32. I used both pandas method and pytorch but no avail. Dataset are from kaggle titanic project. The code is the following:

from torch import nn
import torch
import pandas as pd
import numpy as np
df = pd.DataFrame([[1.1, 2.1], [3.1, 4.1],[5.1,6.1]], columns=['col1', 'col2'])
df.astype('float32')
print(df.dtypes)
df.astype(np.float32)
print(df.dtypes)
a=torch.tensor(df.loc[1][['col1']])
a.to(torch.float32)
print(a)

and get the result

col1    float64
col2    float64
dtype: object
col1    float64
col2    float64
dtype: object
tensor([3.1000], dtype=torch.float64)

I tried to use both method provided by pandas and pytorch. Although it does not raise any errors, it does not work. And I cannot put it into neural network layers as it requires float 32 data type. I also tried to convert them to int, int64, int32 using both method. But nothing works.


Solution

  • Code

    Check out the announcements and make sure to ask questions with minimal and reproducible example. It's harder to get answers if we have to read your long code without a reproducible sample.

    minimal and reproducible example:

    import pandas as pd
    df = pd.DataFrame([[1.1, 2.1], [3.1, 4.1]], columns=['col1', 'col2'])
    

    df:

       col1  col2
    0   1.1   2.1
    1   3.1   4.1
    

    chk dtype of df

    print(df.dtypes)
    

    print:

    col1    float64
    col2    float64
    dtype: object
    

    convert to float32

    out = df.astype('float32')
    print(out.dtypes)
    

    orint:

    col1    float32
    col2    float32
    dtype: object
    

    Most functions in Pandas do not change the original as a result of applying the function.