I want to avoid the possibility of getting decimal points due to a defect in the pandas.

Asked 2 years ago, Updated 2 years ago, 318 views

psycopg2 retrieves data from the database in SQL and framing it into data, when the missing bigint column becomes float.

I would like to return it to DB after processing it with Pandas, but I get an error if it is float.

ERROR:invalid input syntax for type bigint

The reason seems to be that the integer value is also .0.
I tried to read everything by string, but when I changed it from DB to data frame, .0 came with me, so I couldn't help it.

How should I deal with it?
Thank you for your cooperation.

Run Environment
Windows 10 Python 3.7

add
Data framing was avoided by setting dtype=object.
Also
table.asttype({'hoge':np.int64})

ValueError: invalid literal for int() with base 10:
It will be

I am also trying the Int64 that you mentioned in the comment.
I will add it again when I know a little more.

python pandas

2022-09-30 21:51

1 Answers

The missing NaN is treated as float type .
Replacing NaN with fillna and then casting with asttype
Simply replacing with asttype results in ValueError: Cannot convert non-finite values (NA or inf) to integer.
Related Home Questions

import pandas as pd

df = pd.DataFrame({'hoge': [1,2] })
print(df)
""" 
   hoge
1     1
2     2
""" 

df = df.reindex ([0,1,2])
print(df)
""" 
   hoge
1   1.0
2   2.0
3NaN
""" 

# df['hoge'] = df['hoge'].astype('int64')
# ValueError: Cannot convert non-finite values (NA or inf) to integer

df['hoge'] = df['hoge'].filna(0).astype('int64')# The value replaced by filna must be reworked when returning to DB.
print(df)
""" 
   hoge
0     1
1     2
2     0
""" 


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.