0

I am using FeatureClasstoNumPyArray to get information from two features classes

import arcpy
import numpy

sspipe_fcs = 'ssGravityMain'
sspipe_fl = ('ORIGINAL_COST', 'FEATURE', 'DEVELOPMENT','LENGTH_M4')
ssarr = arcpy.da.FeatureClassToNumPyArray(sspipe_fcs, sspipe_fl)
print ssarr
print ssarr.shape
print ssarr.dtype   

wlpipe_fcs = 'Water Mains'
wlpipe_fl = ('ORIGINAL_COST', 'FEATURE', 'DEVELOPMENT','Shape_Length')
wlarr = arcpy.da.FeatureClassToNumPyArray(wlpipe_fcs, wlpipe_fl)
print wlarr
print wlarr.shape
print wlarr.dtype

numpy.concatenate ((ssarr, wlarr))

I would like to combine both arrays but I get an error

TypeError: invalid type promotion

My overall goal is to use the array to eventually prepare some type of pivot table using pandas.

Here are the results of those print statements

[(3087.622879285057, u'Pipe', u'shopping center', 90.81243762603108)
 (4308.758177145577, u'Pipe', u'shopping center', 126.72818168075227)
 (2079.15277816955, u'Pipe', u'shopping center', 61.15155229910441)
 (2055.8024028091863, u'Pipe', u'shopping center', 60.464776553211365)
 (3825.8261621564598, u'Pipe', u'shopping center', 112.5242988869547)
 (5056.951172625341, u'Pipe', u'shopping center', 148.7338580183924)
 (4143.872612491386, u'Pipe', u'shopping center', 121.87860624974664)
 (4545.80563630292, u'Pipe', u'shopping center', 133.70016577361528)
 (3010.085501040413, u'Pipe', u'shopping center', 88.53192650118862)
 (3139.2163487103285, u'Pipe', u'shopping center', 92.32989260912731)
 (3251.2079792976815, u'Pipe', u'shopping center', 95.62376409699063)
 (2104.7324983994613, u'Pipe', u'shopping center', 61.90389701174887)
 (2894.457469764144, u'Pipe', u'shopping center', 85.1311020518866)
 (1922.1176673586654, u'Pipe', u'shopping center', 56.53287256937251)
 (3746.678222477405, u'Pipe', u'shopping center', 110.19641830815897)
 (2629.9600545841795, u'Pipe', u'shopping center', 77.3517663112994)
 (7509.473519849657, u'Pipe', u'shopping center', 220.86686823087229)
 (4927.328877910251, u'Pipe', u'shopping center', 144.92143758559564)
 (5740.15444723423, u'Pipe', u'shopping center', 168.82807197747735)]
(19,)
[('ORIGINAL_COST', '<f8'), ('FEATURE', '<U50'), ('DEVELOPMENT', '<U50'), ('LENGTH_M4', '<f8')]
[(28937.625507077002, u'Pipe', u'shopping center', 826.7893002022)
 (11969.306667602146, u'Pipe', u'shopping center', 341.98019050291845)
 (21051.785700918437, u'Pipe', u'shopping center', 601.4795914548125)]
(3,)
[('ORIGINAL_COST', '<f8'), ('FEATURE', '<U10'), ('DEVELOPMENT', '<U50'), ('Shape_Length', '<f8')]
  • I believe it has something to do with the dtypes of the arrays not coinciding. Can you show us what ssarr.dtype and wlarr.dtype are? – Marcelo Villa-Piñeros Sep 24 '19 at 16:52
  • @MarceloVilla I have updated my post with that information. – GreyHippo Sep 24 '19 at 18:28
1

It seems that you cannot concatenate the two arrays because of two reasons:

  • dtype for FEATURE is different (<U50 vs. <U10).
  • The fourth field name is different (LENGTH_M4 vs. Shape_Length)

You said you wanted to eventually prepare some pivot table using pandas so here is one approach that creates a DataFrame from each array, renames the columns of the second array and then concatenates both DataFrames into a third one:

import arcpy
import numpy as np
import pandas as pd

# get first array
sspipe_fcs = 'ssGravityMain'
sspipe_fl = ('ORIGINAL_COST', 'FEATURE', 'DEVELOPMENT','LENGTH_M4')
ssarr = arcpy.da.FeatureClassToNumPyArray(sspipe_fcs, sspipe_fl)

# get second array
wlpipe_fcs = 'Water Mains'
wlpipe_fl = ('ORIGINAL_COST', 'FEATURE', 'DEVELOPMENT','Shape_Length')
wlarr = arcpy.da.FeatureClassToNumPyArray(wlpipe_fcs, wlpipe_fl)

# convert arrays to DataFrames
dfss = pd.DataFrame(ssarr)
dfwl = pd.DataFrame(wlarr)

# match column names (Shape_Length will become LENGTH_M4)
dfwl.columns = dfss.columns

# create a new concatenated array
df = pd.concat([dfss, dfwl], ignore_index=True)

# check result
df.head()

   ORIGINAL_COST FEATURE      DEVELOPMENT   LENGTH_M4
0    3087.622879    Pipe  shopping center   90.812438
1    4308.758177    Pipe  shopping center  126.728182
2    2079.152778    Pipe  shopping center   61.151552
3    2055.802403    Pipe  shopping center   60.464777
4    3825.826162    Pipe  shopping center  112.524299
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.