'Does astype edit original dataframe?

I have a simple data frame df:

col1 | col2
 7   |  8
 12  |  14

When I check the data types of df by writing df.dtypes, int64 is the data type for both columns.

Now I go to change the data types for col1 by typing

df.astype({'col1': 'float64'}).dtypes

Which returns

col1    float64
col2      int64
dtype: object

Okay so far so good. So now when I double check to see if the data types have changed df...

df.dtypes

The output is

col1    int64
col2    int64
dtype: object

See I thought astype would permanently change the data type of my original df. So I'm quite confused here. Why didn't the data types for df change?



Solution 1:[1]

As per the documentation astype returns a copy, so you could do this:

df = df.astype({'col1': 'float64'})

Alternatively, you could also do:

df.col1 = df.col1.astype('float64')

Solution 2:[2]

The problem here is that the operation is not functioning inplace that's a reason why the argument copy is part of the function.

Furthermore, if you want the change to be permanent, you need to re-define your dataframe.

df = df.astype({'col1': 'float64'})

So that when you check for df.dtypes, col1 will be float.

Solution 3:[3]

to convert all numeric columns from float to int: try this one out

df = df.astype({ x:'int32' for x in df.select_dtypes(include=[np.number]).columns })

this snippet for selecting all numberic columns

df.select_dtypes(include=[np.number]).columns

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Celius Stingher
Solution 3 abdoo