Altair: BUG: alt.datum does not support nested properties

Created on 11 Jun 2018  路  9Comments  路  Source: altair-viz/altair

and those nested properties are typical for topojson/geojson objects; consider example:


boundaries = alt.topo_feature(map_url, 'area')


alt.Chart(boundaries).transform_filter(alt.datum.properties['area_type'] == 'neighborhood').mark_geoshape(fill='red').encode(color='area_id:N').project(
    type="mercator",
    center=[-73.99,40.72]
).properties(width=500,height=300)

bug

Most helpful comment

had to workaround with

MAP = alt.Chart(boundaries).transform_calculate(area_type='datum.properties.area_type',).transform_filter(alt.datum.area_type == 'neighborhood')
MAP.mark_geoshape(fill='red').encode(color='area_id:N').project(
    type="mercator",
    center=[-73.99,40.72]
).properties(width=500,height=300)

All 9 comments

alt.datum['properties.area_type'] == 'neighborhood' is being converted to vega expression

{"filter": "(datum['properties.area_type'] === 'neighborhood')"}

and therefor results in false for every element

had to workaround with

MAP = alt.Chart(boundaries).transform_calculate(area_type='datum.properties.area_type',).transform_filter(alt.datum.area_type == 'neighborhood')
MAP.mark_geoshape(fill='red').encode(color='area_id:N').project(
    type="mercator",
    center=[-73.99,40.72]
).properties(width=500,height=300)

What vega expression do you want it to generate?

It doesn't have to be vega, I am looking for altair equivalent of vega-lite

"transform": [
{"type": "filter", "expr": "datum.properties.area_type == 'neighborhood' "}
]

it works fine when I use alt.datum.property, but not with alt.datum.property.nestedproperty

Sorry to be unclear; "datum.properties.area_type == 'neighborhood' " is an example of a vega expression, which is used within vega-lite.

Currently the alt.datum interface does not support nested properties, but you can pass the vega expression directly as a string, as you found above.

I'll mark this as a bug; we should make the alt.datum interface support nested expressions.

Related: How can alt.datum support a field name that is not a valid Python identifier? For instance, if the field is called acey-ducey, alt.datum.acey-ducey doesn't parse since it thinks the - is a minus sign.

You can pass any string to a getitem, e.g.

alt.datum['acey-ducey']

Running into this as an issue with topojson with properties, which are stored in datum.properties.key.

VegaLite:

  "data": {
    "url": "https://gist.githubusercontent.com/manzt/9e0f8301c929b3b95ebcbc664c1e5761/raw/a1527d11fe2bc4a0462d533fd679e216cf09a465/nyschools-topo.json",
    "format": {
      "type": "topojson",
      "feature": "zones"
    },
    "transform": [{"filter": {"field": "properties.dbn", "valid": true}}],
  },

Altair:

nyc_url = (
    f"https://gist.githubusercontent.com/manzt/"
    f"9e0f8301c929b3b95ebcbc664c1e5761/raw/"
    f"a1527d11fe2bc4a0462d533fd679e216cf09a465/"
    f"nyschools-topo.json"
)
nyc_zones = alt.topo_feature(nyc_url, 'zones')

alt.Chart(nyc_zones).mark_geoshape(
    fill='lightgray',
    stroke='white'
).transform_filter(
    alt.FieldValidPredicate(field="properties.dbn", valid=True)
).encode(
    fill = "properties.boro:N",
)
# nothing rendered

I don't think your failure is related to this issue: you never use alt.datum. I'd suggest debugging via the vega editor.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breadbaron picture breadbaron  路  4Comments

nielsmde picture nielsmde  路  4Comments

bmcfee picture bmcfee  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments

galloramiro picture galloramiro  路  3Comments