pyreadstat icon indicating copy to clipboard operation
pyreadstat copied to clipboard

AttributeError: 'float' object has no attribute 'encode'

Open lapalus opened this issue 2 months ago • 2 comments

I have upgraded pyreadstat to 1.3.x and encounter an error from code that previously worked. I have a very large dataset and it is hard to share. Problem occurs from version 1.3.0 and is present in all following versions.

Does anyone know of any change from 1.3.0 -> that can explain this?

The error i get is the following:

3857 # Write sav file to the temporary file
-> 3858 pyreadstat.write_sav(
   3859     data,
   3860     temp_path,
   3861     compress=True,
   3862     variable_value_labels=value_labels,
   3863     column_labels=variable_labels,
   3864     variable_measure=variable_measures,
   3865     variable_display_width=variable_display_width,
   3866     variable_format=variable_formats
   3867 )
   3869 temp_file.seek(0)
   3870 sav_content = temp_file.read()

File pyreadstat/pyreadstat.pyx:784, in pyreadstat.pyreadstat.write_sav()

File pyreadstat/_readstat_writer.pyx:730, in pyreadstat._readstat_writer.run_write()

File pyreadstat/_readstat_writer.pyx:333, in pyreadstat._readstat_writer.get_narwhals_column_types()

File pyreadstat/_readstat_writer.pyx:227, in pyreadstat._readstat_writer.get_narwhals_str_series_max_length()

AttributeError: 'float' object has no attribute 'encode'

lapalus avatar Nov 18 '25 08:11 lapalus

This is because now a third party package, narwhals is used to detect the column types in the dataframe. So, one of the columns is not correctly detected.

In order to fix, I need to reproduce. Therefore please share data to reproduce. From your dataset you can take a slice of a few rows, the important thing is that I can see the column types. You can also replace the data if sensitive preserving the column types.

ofajardo avatar Nov 18 '25 09:11 ofajardo

Thank you for swift answer @ofajardo. Yes I thought there where something like that and we probably have some error somewhere creating mixed values in some column. We put in our backlog to look into. For now we will downgrade production to make it work.

lapalus avatar Nov 18 '25 09:11 lapalus