pandasnumpypint

Convert pint-panda dataframe to numpy


I have a large dataframe (>30 000 rows and > 500 columns), I am using pint to convert from one unit to another using the pint-panda option then I wish to write the dataframe to a text file. I have up to 20 000 text files to write.

However, I couldn't find in the pint documentation how to "remove" the pint layer once the conversion is done. For speed reasons I am using np.savetxt() rather than a panda solution. To that end, I first convert my dataframe to float df.astype(float) and then apply the desired float formating in the text file.

np.savetxt( file, output_data.astype(float), header=header_out, delimiter="", fmt="%-15.5e", comments="", )

However doing that generates as many warning messages as there are columns: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray

I am using an ugly workaround warnings.filterwarnings("ignore") to avoid being spammed but it is not the best solution.

Using df.to_numpy() yields a pint Quantity object which is incompatible with the float foramting in np.savetxt.

How could I convert my dataframe to float without generating all those warning messages ? Or remove the pint effect on my data ?


Solution

  • After digging even more, it turns out that the pint-pandas package had a recent update. The warning message mentioned has been removed from v0.6.