Amen_90
Amen_90

Reputation: 350

input must be an array, list, tuple or scalar pyproj

I Have a DF in which I am trying to convert the eastings/northings to long/lats. My df looks like this:

import pandas as pd
import numpy as np
import pyproj

    Postcode    Eastings    Northings
0   AB101AB     394235      806529
1   AB101AF     394181      806429
2   AB101AG     394230      806469
3   AB101AH     394371      806359
4   AB101AL     394296      806581

I am using a well know code block to convert the eastings and northings to long/lats and add those long/lats as new columns to the df:

def proj_transform(df):
    bng = pyproj.Proj("+init=EPSG:27700")
    wgs84 = pyproj.Proj("+init=EPSG:4326")
    lats = pd.Series()
    lons = pd.Series()
    for idx, val in enumerate(df['Eastings']):
        lon, lat = pyproj.transform(bng, wgs84, df['Eastings'][idx], df['Northings'][idx])
        lats.set_value(idx, lat)
        lons.set_value(idx, lon)
    df['lat'] = lats
    df['lon'] = lons
    return df

df_transform = proj_transform(my_df)

However, I keep getting the following error, "input must be an array, list, tuple or scalar". Does anyone have any insight into where I am going wrong here?

Upvotes: 1

Views: 905

Answers (2)

snowman2
snowman2

Reputation: 711

This is the fastest method:

https://gis.stackexchange.com/a/334307/144357

from pyproj import Transformer

trans = Transformer.from_crs(
    "EPSG:27700",
    "EPSG:4326",
    always_xy=True,
)
xx, yy = trans.transform(my_df["Eastings"].values, my_df["Northings"].values)
my_df["X"] = xx
my_df["Y"] = yy

Also helpful for reference:

Upvotes: 2

jezrael
jezrael

Reputation: 863156

You can use DataFrame.apply with axis=1 and change function like:

def proj_transform(x):
    e = x['Eastings']
    n = x['Northings']
    bng = pyproj.Proj("+init=EPSG:27700")
    wgs84 = pyproj.Proj("+init=EPSG:4326")
    lon, lat = pyproj.transform(bng, wgs84, e, n)

    return pd.Series([lon, lat])

my_df[['lat','lon']] = my_df.apply(proj_transform, axis=1)

Upvotes: 1

Related Questions