diff --git a/README.md b/README.md index fb035f40..82535d82 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ pip install datascience This project adheres to [Semantic Versioning](http://semver.org/). +### v0.12.1 +* Adds an `shallow` argument to `Table#select` to allow for shallow +copying of the table's data. + ### v0.12.0 * Changes `Table#scatter`'s argument name of `colors` to `group` to mirror `Table#hist`. * Makes a grouped scatterplot's legend identical to a group histogram's legend. diff --git a/datascience/tables.py b/datascience/tables.py index 3cb9262e..74ea455d 100644 --- a/datascience/tables.py +++ b/datascience/tables.py @@ -596,12 +596,14 @@ def copy(self, *, shallow=False): self._add_column_and_format(table, label, column) return table - def select(self, *column_or_columns): + def select(self, *column_or_columns, shallow=False): """Return a table with only the columns in ``column_or_columns``. Args: ``column_or_columns``: Columns to select from the ``Table`` as either column labels (``str``) or column indices (``int``). + ''shallow'': Setting this to True will select columns without + making a copy of the column. Returns: A new instance of ``Table`` containing only selected columns. @@ -644,7 +646,10 @@ def select(self, *column_or_columns): labels = self._varargs_as_labels(column_or_columns) table = type(self)() for label in labels: - self._add_column_and_format(table, label, np.copy(self[label])) + if shallow: + self._add_column_and_format(table, label, self[label]) + else: + self._add_column_and_format(table, label, np.copy(self[label])) return table # These, along with a snippet below, are necessary for Sphinx to diff --git a/datascience/version.py b/datascience/version.py index 2c7bffbf..f8d90954 100644 --- a/datascience/version.py +++ b/datascience/version.py @@ -1 +1 @@ -__version__ = '0.12.0' +__version__ = '0.12.1' diff --git a/tests/test_tables.py b/tests/test_tables.py index d0b2cee6..36068174 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -155,6 +155,40 @@ def test_select(table): 10 | 1 """) +def test_select_with_copy_values(table): + t = table.copy() + test = t.select(["points"], shallow=True) + test1 = t.select(["points"], shallow=False) #default setting + assert_equal(test, """ + points + 1 + 2 + 2 + 10 + """) + t["points"][0] = 999 + assert_equal(t.select("points"), """ + points + 999 + 2 + 2 + 10 + """) + assert_equal(test, """ + points + 999 + 2 + 2 + 10 + """) + assert_equal(test1, """ + points + 1 + 2 + 2 + 10 + """) + def test_drop(table): t = table test = t.drop(['points', 1])