nithinmurali / pygsheets

Google Sheets Python API v4
https://pygsheets.readthedocs.io/en/latest
Other
1.51k stars 220 forks source link

get_as_df() with ValueRenderOption.UNFORMATTED_VALUE truncates floats to int #497

Closed janfrederik closed 3 years ago

janfrederik commented 3 years ago

Problem get_as_df(value_render=ValueRenderOption.UNFORMATTED_VALUE) converts floats to int.

To Reproduce

worksheet = spreadsheet[0]
df = worksheet.get_as_df(has_header=False, value_render=pygsheets.custom_types.ValueRenderOption.UNFORMATTED_VALUE)
print(df)

# This returns
#        0
# 0  34521

df.dtypes
# 0    int64
# dtype: object

So the float is converted to an int. I lost my decimal information.

System Information

Cause: numericise() is structured to only receive strings as input. However, when using ValueRenderOption.UNFORMATTED_VALUE, numericise() receives ints or floats. Doing int(value) on a float value gives no error, hence an int is returned.

Workaround Calling get_as_df() with numerize=False works, but then empty values are not converted anymore and I need this.

Proposed solution Adapt numericise(): if value is not a string, return it as is. Only apply the conversion logic if value is a string.