I've made an interesting finding with SpatialJoin_analysis's field mapping. It appears that there are some undocumented merge rules that produce correct output.
My scenario is as follows. I have one layer that contains points with several fields. The second layer contains a grid that I would like to aggregate the points from the first layer into. To accomplish this I am calling the SpatialJoin_analysis operation:
arcpy.SpatialJoin_analysis(
layer_grid,
curr_layer,
new_layer_path,
join_operation="JOIN_ONE_TO_ONE",
join_type="KEEP_COMMON",
match_option="INTERSECT",
field_mapping=get_field_mapping(curr_layer))
where the output of the get_field_mapping(curr_layer) method returns a valid field mapping.
def get_field_mapping(layer):
"""Creates a mapping between selected fields from the origin GDB to the
output GDB. The output fields will aggregate multiple entries either by
a sum or count operation.
"""
fm_value_16 = arcpy.FieldMap()
fm_vreg = arcpy.FieldMap()
intsct_mapping = arcpy.FieldMappings()
fm_value_16.addInputField(layer, "PRICE_2016_ADJUSTED")
fm_value_16.mergeRule = "Sum"
fm_out_val_16 = arcpy.Field()
fm_out_val_16.name = "Value_2016_Sum"
fm_out_val_16.type = "Double"
fm_value_16.outputField = fm_out_val_16
fm_vreg.addInputField(layer, "V_REG_NUMBER")
fm_vreg.mergeRule = "Unique"
fm_out_vreg = arcpy.Field()
fm_out_vreg.name = "VRN_Unique_Count"
fm_out_vreg.type = "Integer"
fm_vreg.outputField = fm_out_vreg
intsct_mapping.addFieldMap(fm_value_16)
intsct_mapping.addFieldMap(fm_vreg)
return intsct_mapping
Each point in the first layer has two fields, PRICE_2016_ADJUSTED and V_REG_NUMBER, that I want merged. For PRICE_2016_ADJUSTED I want its values to be summed, while for V_REG_NUMBER I want to calculate the number of unique values. Essentially, I want functionality similar to the Sum and Unique rules from (https://pro.arcgis.com/en/pro-app/2.9/tool-reference/analysis/summary-statistics.htm). However, according to the documentation for fieldMap (https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/fieldmap.htm) I'm restricted to only using "Sum" as a valid mergeRule.
Yet, this line (fm_vreg.mergeRule = "Unique") does in fact produce the number of unique entries in the aggregated point! Is this behaviour intentional and if so, can this be used in production code?
Awesome find! If you're not comfortable using the undocumented merge rule in your production code, maybe you could use a Join merge rule into a text field, then follow up your spatial join with a calculate field or update cursor to parse out the number of unique values in each string (split it into a list on the join delimiter, then convert the list to a set and find the set's length).