Search code examples
pythoncsvpyproj

Python - Pyproj Transform Returns inf for Blank Cell in CSV. How to ignore blank cell?


My Python code converts latitude and longitude using pyproj. It reads this from an existing CSV file and writes a new CSV file with new code.

When it comes across a blank cell within the existing CSV file it writes in the cell of the new CSV file 'inf'

How can I stop it writing anything and leave the cell blank?

Current Python Code

from pyproj import Transformer
import pandas

pdf = pandas.read_csv("existing_file.csv", encoding='ANSI')
transformer = Transformer.from_crs("epsg:99999", "epsg:8888", always_xy=True)
xx, yy = transformer.transform(pdf["longitude"].values, pdf["latitude"].values)
pdf = pdf.assign(longitude=xx, latitude=yy)
pdf.to_csv("new_file.csv")

Solution

  • Found a solution here: dropping infinite values from dataframes in pandas?

    I think the solution is to replace the inf values with nan:

    pdf = pdf.replace([np.inf, -np.inf], np.nan)