Repository: altair-viz/altair
Pull request #2099 (2020-04-19 03:37:11)

Fix item access for expressions
See #2096


Patch:
diff --git a/README.md b/README.md
index 28f2f9d6..6bea068e 100644
--- a/README.md
+++ b/README.md
@@ -249,14 +249,12 @@ See [`CONTRIBUTING.md`](https://github.com/altair-viz/altair/blob/master/CONTRIB
 If you use Altair in academic work, please consider citing http://joss.theoj.org/papers/10.21105/joss.01057 as
 
 ```bib
-@article{VanderPlas2018,
+@article{2018-altair,
   doi = {10.21105/joss.01057},
   url = {https://doi.org/10.21105/joss.01057},
-  year = {2018},
+  year  = {2018},
+  month = {dec},
   publisher = {The Open Journal},
-  volume = {3},
-  number = {32},
-  pages = {1057},
   author = {Jacob VanderPlas and Brian Granger and Jeffrey Heer and Dominik Moritz and Kanit Wongsuphasawat and Arvind Satyanarayan and Eitan Lees and Ilia Timofeev and Ben Welsh and Scott Sievert},
   title = {Altair: Interactive Statistical Visualizations for Python},
   journal = {Journal of Open Source Software}
@@ -264,16 +262,13 @@ If you use Altair in academic work, please consider citing http://joss.theoj.org
 ```
 Please additionally consider citing the [vega-lite](http://vega.github.io/vega-lite/) project, which Altair is based on: https://dl.acm.org/doi/10.1109/TVCG.2016.2599030
 ```bib
-@article{Satyanarayan2017,
-author={Satyanarayan, Arvind and Moritz, Dominik and Wongsuphasawat, Kanit and Heer, Jeffrey},
-title={Vega-Lite: A Grammar of Interactive Graphics},
-journal={IEEE transactions on visualization and computer graphics},
-year={2017},
-volume={23},
-number={1},
-pages={341-350},
-publisher={IEEE}
-} 
+@article{2017-vega-lite,
+ title = {Vega-Lite: A Grammar of Interactive Graphics},
+ author = {Arvind Satyanarayan AND Dominik Moritz AND Kanit Wongsuphasawat AND Jeffrey Heer},
+ journal = {IEEE Trans. Visualization \& Comp. Graphics (Proc. InfoVis)},
+ year = {2017},
+ url = {http://idl.cs.washington.edu/papers/vega-lite},
+}
 ```
 
 ## Whence Altair?
diff --git a/altair/examples/bar_with_rolling_mean.py b/altair/examples/bar_with_rolling_mean.py
deleted file mode 100644
index 1f7d9033..00000000
--- a/altair/examples/bar_with_rolling_mean.py
+++ /dev/null
@@ -1,27 +0,0 @@
-"""
-Bar Chart with Rolling Mean
----------------------------
-A bar chart overlayed with a rolling mean. In this example the average of values over the previous decade is displayed as a line.
-"""
-# category: bar charts
-import altair as alt
-from vega_datasets import data
-
-source = data.wheat()
-
-bar = alt.Chart(source).mark_bar().encode(
-    x='year:O',
-    y='wheat:Q'
-)
-
-line = alt.Chart(source).mark_line(color='red').transform_window(
-    # The field to average
-    rolling_mean='mean(wheat)',
-    # The number of values before and after the current value to include.
-    frame=[-9, 0]
-).encode(
-    x='year:O',
-    y='rolling_mean:Q'
-)
-
-(bar + line).properties(width=600)
diff --git a/altair/examples/line_chart_with_cumsum.py b/altair/examples/line_chart_with_cumsum.py
deleted file mode 100644
index 2b21b084..00000000
--- a/altair/examples/line_chart_with_cumsum.py
+++ /dev/null
@@ -1,24 +0,0 @@
-"""
-Line Chart with Cumulative Sum
-------------------------------
-This chart creates a simple line chart from the cumulative sum of a fields.
-"""
-# category: line charts
-import altair as alt
-from vega_datasets import data
-
-source = data.wheat()
-
-alt.Chart(source).mark_line().transform_window(
-    # Sort the data chronologically
-    sort=[{'field': 'year'}],
-    # Include all previous records before the current record and none after
-    # (This is the default value so you could skip it and it would still work.)
-    frame=[None, 0],
-    # What to add up as you go
-    cumulative_wheat='sum(wheat)'
-).encode(
-    x='year:O',
-    # Plot the calculated field created by the transformation
-    y='cumulative_wheat:Q'
-).properties(width=600)
\ No newline at end of file
diff --git a/altair/examples/line_with_log_scale.py b/altair/examples/line_with_log_scale.py
deleted file mode 100644
index a47cd40c..00000000
--- a/altair/examples/line_with_log_scale.py
+++ /dev/null
@@ -1,18 +0,0 @@
-"""
-Line Chart with Logarithmic Scale
----------------------------------
-How to make a line chart on a `<Logarithmic scale> https://en.wikipedia.org/wiki/Logarithmic_scale`_.
-"""
-# category: line charts
-import altair as alt
-from vega_datasets import data
-
-source = data.population()
-
-alt.Chart(source).mark_line().encode(
-    x='year:O',
-    y=alt.Y(
-        'sum(people)',
-        scale=alt.Scale(type="log")  # Here the scale is applied
-    )
-)
\ No newline at end of file
diff --git a/altair/expr/core.py b/altair/expr/core.py
index 8d027989..c1fdf49b 100644
--- a/altair/expr/core.py
+++ b/altair/expr/core.py
@@ -134,6 +134,10 @@ class Expression(SchemaBase):
     def __invert__(self):
         return UnaryExpression("!", self)
 
+    # item access
+    def __getitem__(self, val):
+        return GetItemExpression(self, val)
+
 
 class UnaryExpression(Expression):
     def __init__(self, op, val):
@@ -184,4 +188,4 @@ class GetItemExpression(Expression):
         super(GetItemExpression, self).__init__(group=group, name=name)
 
     def __repr__(self):
-        return "{}['{}']".format(self.group, self.name)
+        return "{}[{!r}]".format(self.group, self.name)
diff --git a/doc/conf.py b/doc/conf.py
index 3a39289f..4d0c9a89 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -65,7 +65,7 @@ master_doc = "index"
 
 # General information about the project.
 project = "Altair"
-copyright = "2016-2020, Altair Developers"
+copyright = "2016-2019, Altair Developers"
 author = "Brian Granger and Jake VanderPlas"
 
 # The version info for the project you're documenting, acts as replacement for
diff --git a/doc/user_guide/customization.rst b/doc/user_guide/customization.rst
index e128a69b..1477a62a 100644
--- a/doc/user_guide/customization.rst
+++ b/doc/user_guide/customization.rst
@@ -123,10 +123,10 @@ just before the chart is rendered.
 
 Adjusting Axis Limits
 ---------------------
-The default axis limit used by Altair is dependent on the type of the data.
-To fine-tune the axis limits beyond these defaults, you can use the
-:class:`Scale` property of the axis encodings. For example, consider the
-following plot:
+The default axis limit used by Altair is dependent on the type of the data
+(see, for example, :ref:`type-axis-scale`). To fine-tune the axis limits
+beyond these defaults, you can use the :class:`Scale` property of the axis
+encodings. For example, consider the following plot:
 
 .. altair-plot::
 
diff --git a/doc/user_guide/encoding.rst b/doc/user_guide/encoding.rst
index 22f80451..76cf8fd9 100644
--- a/doc/user_guide/encoding.rst
+++ b/doc/user_guide/encoding.rst
@@ -226,6 +226,21 @@ that contains integers specifying a year:
         base.encode(x='year:O').properties(title='year=ordinal')
     )
 
+In altair, quantitative scales always start at zero unless otherwise
+specified, while ordinal scales are limited to the values within the data.
+
+Overriding the behavior of including zero in the axis, we see that even then
+the precise appearance of the marks representing the data are affected by
+the data type:
+
+.. altair-plot::
+
+    base.encode(
+        alt.X('year:Q',
+            scale=alt.Scale(zero=False)
+        )
+    )
+
 Because quantitative values do not have an inherent width, the bars do not
 fill the entire space between the values.
 This view also makes clear the missing year of data that was not immediately
diff --git a/doc/user_guide/faq.rst b/doc/user_guide/faq.rst
index 4b52ce88..3ce5224c 100644
--- a/doc/user_guide/faq.rst
+++ b/doc/user_guide/faq.rst
@@ -158,7 +158,7 @@ and performance may suffer.
 Passing Data by URL
 ~~~~~~~~~~~~~~~~~~~
 A better solution when working with large datasets is to not embed the data
-in the notebook, but rather store it separately and pass it to the chart by URL.
+in the notebook, but rather store it separately pass it to the chart by URL.
 This not only addresses the issue of large notebooks, but also leads to better
 interactivity performance with large datasets.
 
diff --git a/doc/user_guide/interactions.rst b/doc/user_guide/interactions.rst
index eecff9c9..8080c107 100644
--- a/doc/user_guide/interactions.rst
+++ b/doc/user_guide/interactions.rst
@@ -308,7 +308,7 @@ used to determine which points are part of the selection.
 
 For example, here we create a small chart that acts as an interactive legend,
 by targeting the Origin field using ``fields=['Origin']``. Clicking on points
-in the upper-right plot (the legend) will propagate a selection for all points
+in the upper-left plot (the legend) will propagate a selection for all points
 with a matching ``Origin``.
 
 .. altair-plot::
@@ -375,8 +375,8 @@ Binding: Adding Data Driven Inputs
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 With an understanding of the selection types and conditions, you can now add data-driven input elements to the charts using the ``bind`` option. As specified by `Vega-lite binding <https://vega.github.io/vega-lite/docs/bind.html#input-element-binding>`_, selections can be bound two-ways:
 
-1. Single selections can be bound directly to an input element, *for example, a radio button.*
-2. Interval selections which can be bound to scale, *for example, zooming in on a map.*
+1. Single selections can be bound directly to an input elements. *For example, a radio button.*
+2. Interval selections which can be bound to scale. *for example, zooming in on a map.*
 
 Input Element Binding
 ^^^^^^^^^^^^^^^^^^^^^


Test patch:
diff --git a/altair/expr/tests/test_expr.py b/altair/expr/tests/test_expr.py
index 9da9177c..17a2b98f 100644
--- a/altair/expr/tests/test_expr.py
+++ b/altair/expr/tests/test_expr.py
@@ -94,3 +94,8 @@ def test_copy():
 def test_datum_getattr():
     x = datum["foo"]
     assert repr(x) == "datum['foo']"
+
+
+def test_expression_getitem():
+    x = datum.foo[0]
+    assert repr(x) == "datum.foo[0]"