Repository: IAMconsortium/pyam
Pull request #713 (2022-12-02 19:12:12)

Added (useful) error for using lists in filter by level
…rbidden column.

# Please confirm that this PR has done the following:

- [x] Tests Added
- [x] Documentation Added
- [x] Name of contributors Added to AUTHORS.rst
- [x] Description in RELEASE_NOTES.md Added

## Adding to RELEASE_NOTES.md (remove section after adding to RELEASE_NOTES.md)

Please add a single line in the release notes similar to the following:

```
- (#XX)[http://link-to-pr.com] Added feature which does something
```

# Description of PR

Please describe the changes introduced by this PR.


codecov[bot]: # [Codecov](https://codecov.io/gh/IAMconsortium/pyam/pull/713?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) Report
> Merging [#713](https://codecov.io/gh/IAMconsortium/pyam/pull/713?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) (95e7f4b) into [main](https://codecov.io/gh/IAMconsortium/pyam/commit/83b75423de26e3a322dcd1ea9c61069d234f4dc8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) (83b7542) will **increase** coverage by `0.0%`.
> The diff coverage is `100.0%`.

```diff
@@          Coverage Diff          @@
##            main    #713   +/-   ##
=====================================
  Coverage   94.9%   94.9%           
=====================================
  Files         58      58           
  Lines       5909    5914    +5     
=====================================
+ Hits        5609    5614    +5     
  Misses       300     300           
```


| [Impacted Files](https://codecov.io/gh/IAMconsortium/pyam/pull/713?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) | Coverage Δ | |
|---|---|---|
| [pyam/\_style.py](https://codecov.io/gh/IAMconsortium/pyam/pull/713/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS9fc3R5bGUucHk=) | `49.1% <ø> (ø)` | |
| [pyam/utils.py](https://codecov.io/gh/IAMconsortium/pyam/pull/713/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS91dGlscy5weQ==) | `91.7% <100.0%> (+<0.1%)` | :arrow_up: |
| [tests/test\_core.py](https://codecov.io/gh/IAMconsortium/pyam/pull/713/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-dGVzdHMvdGVzdF9jb3JlLnB5) | `100.0% <100.0%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium)

Rlamboll: @danielhuppmann The tests indicate a problem with the cached dependencies, I don't think this is related to my changes though?
danielhuppmann: > @danielhuppmann The tests indicate a problem with the cached dependencies, I don't think this is related to my changes though?

Fixed via #714

Patch:
diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml
index 70e1836..1188ad4 100644
--- a/.github/workflows/build-docs.yml
+++ b/.github/workflows/build-docs.yml
@@ -28,7 +28,7 @@ jobs:
     - uses: actions/checkout@v3
 
     - name: Set up Python ${{ matrix.python-version }}
-      uses: actions/setup-python@v3
+      uses: actions/setup-python@v4
       with:
         python-version: ${{ matrix.python-version }}
 
diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml
index 74c8a16..cb28901 100644
--- a/.github/workflows/nightly.yml
+++ b/.github/workflows/nightly.yml
@@ -30,7 +30,7 @@ jobs:
     - uses: actions/checkout@v3
 
     - name: Set up Python ${{ matrix.python-version }}
-      uses: actions/setup-python@v3
+      uses: actions/setup-python@v4
       with:
         python-version: ${{ matrix.python-version }}
 
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index f66f7de..147d1d6 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -15,9 +15,9 @@ jobs:
     runs-on: ubuntu-latest
 
     steps:
-    - uses: actions/checkout@v2
+    - uses: actions/checkout@v3
 
-    - uses: actions/setup-python@v2
+    - uses: actions/setup-python@v4
 
     - name: Cache Python packages
       uses: actions/cache@v2
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 5db0c6d..b44cacb 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -7,6 +7,7 @@ Bump minimum version of **pandas** to v1.2.0 to support automatic engine selecti
 
 ## Individual updates
 
+- [#713](https://github.com/IAMconsortium/pyam/pull/713) Informative error when using lists for filter by level. `Level` now a forbidden column. 
 - [#709](https://github.com/IAMconsortium/pyam/pull/709) Hotfix ops to support `fillna=0`
 - [#708](https://github.com/IAMconsortium/pyam/pull/708) Remove 'xls' as by-default-supported file format
 
diff --git a/pyam/_style.py b/pyam/_style.py
index 66dcbbd..4cad84d 100644
--- a/pyam/_style.py
+++ b/pyam/_style.py
@@ -50,7 +50,7 @@ def _get_standard_colors(
         elif color_type == "random":
 
             def random_color(column):
-                """ Returns a random color represented as a list of length 3"""
+                """Returns a random color represented as a list of length 3"""
                 # GH17525 use common._random_state to avoid resetting the seed
                 rs = com.random_state(column)
                 return rs.rand(3).tolist()
diff --git a/pyam/utils.py b/pyam/utils.py
index 4ddd980..d1f9de0 100644
--- a/pyam/utils.py
+++ b/pyam/utils.py
@@ -31,7 +31,7 @@ LONG_IDX = IAMC_IDX + ["year"]
 REQUIRED_COLS = ["region", "variable", "unit"]
 
 # illegal terms for data/meta column names to prevent attribute conflicts
-ILLEGAL_COLS = ["data", "meta"]
+ILLEGAL_COLS = ["data", "meta", "level"]
 
 # dictionary to translate column count to Excel column names
 NUMERIC_TO_STR = dict(
@@ -428,6 +428,11 @@ def find_depth(data, s="", level=None):
         whether depth satisfies the condition (equality if level is int,
         >= if ``.+``,  <= if ``.-``)
     """
+    if islistable(level):
+        raise ValueError(
+            "Level is only run with ints or strings, not lists. Use strings with "
+            "integers and + or - to filter by ranges."
+        )
     if isstr(data):
         return _find_depth([data], s, level)[0]
 


Test patch:
diff --git a/.github/workflows/pytest-dependency.yml b/.github/workflows/pytest-legacy.yml
similarity index 80%
rename from .github/workflows/pytest-dependency.yml
rename to .github/workflows/pytest-legacy.yml
index 6da32fc..c882b4d 100644
--- a/.github/workflows/pytest-dependency.yml
+++ b/.github/workflows/pytest-legacy.yml
@@ -1,7 +1,7 @@
-# This workflow installs the package and runs the tests with older versions of dependencies
+# This workflow runs the tests with the oldest explicitly supported versions of dependencies
 # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
 
-name: pytest-dependency
+name: pytest-legacy
 
 on:
   push:
@@ -10,7 +10,7 @@ on:
     branches: [ '**' ]
 
 jobs:
-  pytest-dependency:
+  pytest-legacy:
 
     runs-on: ubuntu-latest
 
@@ -18,9 +18,9 @@ jobs:
     - uses: actions/checkout@v3
 
     - name: Set up Python
-      uses: actions/setup-python@v3
+      uses: actions/setup-python@v4
       with:
-        python-version: '3.7.1'
+        python-version: '3.7.13'
 
     - name: Install specific out-dated version of dependencies
       # Update the package requirements when changing minimum dependency versions
diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml
index ed7347a..f49c5c0 100644
--- a/.github/workflows/pytest.yml
+++ b/.github/workflows/pytest.yml
@@ -33,7 +33,7 @@ jobs:
     - uses: actions/checkout@v3
 
     - name: Set up Python ${{ matrix.python-version }}
-      uses: actions/setup-python@v3
+      uses: actions/setup-python@v4
       with:
         python-version: ${{ matrix.python-version }}
 
diff --git a/tests/test_core.py b/tests/test_core.py
index 2b79e1f..939c473 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -383,6 +383,11 @@ def test_variable_depth_raises(test_df):
     pytest.raises(ValueError, test_df.filter, level="1/")
 
 
+def test_variable_depth_with_list_raises(test_df):
+    pytest.raises(ValueError, test_df.filter, level=["1", "2"])
+    pytest.raises(ValueError, test_df.filter, level=[1, 2])
+
+
 def test_timeseries(test_df):
     dct = {
         "model": ["model_a"] * 2,