Pandas: astype(int) converts all values to -2147483648
Question:
I am converting large numbers to int
using astype()
.
import pandas as pd
x = [56668772800.0, 55899926600.0, 55038007900.0, 58073681300.0, 58224458500.0]
df = pd.DataFrame(x, columns=["pol_number"])
df.pol_number = df.pol_number.astype(int)
df
And all five values have become -2147483648.
I assume the numbers are too big for type int
but long int
and big int
don’t compile.
Answers:
You can use numpy instead of regular Python integers, since numpy has a wider range of data types, including int64, which can represent large integers:
import pandas as pd
import numpy as np
x = [56668772800, 55899926600, 55038007900, 58073681300, 58224458500]
df = pd.DataFrame(np.array(x, dtype=np.int64), columns=["pol_number"])
print(df)
Try using int64:
import pandas as pd
x = [56668772800.0, 55899926600.0, 55038007900.0, 58073681300.0, 58224458500.0]
df = pd.DataFrame(x, columns=["pol_number"])
df.pol_number = df.pol_number.astype('int64')
df
Output:
pol_number
0 56668772800
1 55899926600
2 55038007900
3 58073681300
4 58224458500
I am converting large numbers to int
using astype()
.
import pandas as pd
x = [56668772800.0, 55899926600.0, 55038007900.0, 58073681300.0, 58224458500.0]
df = pd.DataFrame(x, columns=["pol_number"])
df.pol_number = df.pol_number.astype(int)
df
And all five values have become -2147483648.
I assume the numbers are too big for type int
but long int
and big int
don’t compile.
You can use numpy instead of regular Python integers, since numpy has a wider range of data types, including int64, which can represent large integers:
import pandas as pd
import numpy as np
x = [56668772800, 55899926600, 55038007900, 58073681300, 58224458500]
df = pd.DataFrame(np.array(x, dtype=np.int64), columns=["pol_number"])
print(df)
Try using int64:
import pandas as pd
x = [56668772800.0, 55899926600.0, 55038007900.0, 58073681300.0, 58224458500.0]
df = pd.DataFrame(x, columns=["pol_number"])
df.pol_number = df.pol_number.astype('int64')
df
Output:
pol_number
0 56668772800
1 55899926600
2 55038007900
3 58073681300
4 58224458500