Repository: IAMconsortium/pyam

Issue:
Reading filtered dataframes from iiasa contain incorrect properties
When reading from the IIASA database, the returned database seems to always have .model, .scenario and .variable properties of the unfiltered database, even when we request only a subset of data. 

`new_read = read_iiasa(
        "iamc15", scenario="SSP2*", model="MESSAGE*", variable="*CO2" 
 )
print(new_read.scenario)
print(new_read.variable)`
investigate the properties 

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

```diff
@@          Coverage Diff          @@
##            main    #695   +/-   ##
=====================================
  Coverage   94.8%   94.8%           
=====================================
  Files         58      58           
  Lines       5819    5823    +4     
=====================================
+ Hits        5521    5525    +4     
  Misses       298     298           
```


| [Impacted Files](https://codecov.io/gh/IAMconsortium/pyam/pull/695?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) | Coverage Δ | |
|---|---|---|
| [pyam/core.py](https://codecov.io/gh/IAMconsortium/pyam/pull/695/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS9jb3JlLnB5) | `94.8% <100.0%> (+<0.1%)` | :arrow_up: |
| [tests/test\_core.py](https://codecov.io/gh/IAMconsortium/pyam/pull/695/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%> (ø)` | |
| [tests/test\_iiasa.py](https://codecov.io/gh/IAMconsortium/pyam/pull/695/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-dGVzdHMvdGVzdF9paWFzYS5weQ==) | `97.2% <100.0%> (+<0.1%)` | :arrow_up: |

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)


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index a16b12d..1f98daf 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -2,6 +2,7 @@
 
 ## Individual updates
 
+- [#695](https://github.com/IAMconsortium/pyam/pull/695) Remove unused meta levels during initialization
 - [#688](https://github.com/IAMconsortium/pyam/pull/688) Remove ixmp as optional dependency
 - [#684](https://github.com/IAMconsortium/pyam/pull/684) Use new IIASA-manager API with token refresh 
 - [#679](https://github.com/IAMconsortium/pyam/pull/679) `set_meta()` now supports pandas.DataFrame as an argument
diff --git a/pyam/core.py b/pyam/core.py
index 73754ad..0df3995 100755
--- a/pyam/core.py
+++ b/pyam/core.py
@@ -806,7 +806,9 @@ class IamDataFrame(object):
                 meta = meta.rename(
                     columns={i.capitalize(): i for i in META_IDX}
                 ).set_index(self.meta.index.names)
+
             meta = meta.loc[self.meta.index.intersection(meta.index)]
+            meta.index = meta.index.remove_unused_levels()
             self.meta = merge_meta(meta, self.meta, ignore_conflict=True)
             return
 
@@ -830,7 +832,7 @@ class IamDataFrame(object):
         # if no valid index is provided, add meta as new column `name` and exit
         if index is None:
             self.meta[name] = list(meta) if islistable(meta) else meta
-            return  # EXIT FUNCTION
+            return
 
         # use meta.index if index arg is an IamDataFrame
         if isinstance(index, IamDataFrame):
@@ -917,7 +919,7 @@ class IamDataFrame(object):
 
         if len(idx) == 0:
             logger.info("No scenarios satisfy the criteria")
-            return  # EXIT FUNCTION
+            return
 
         # update meta dataframe
         self._new_meta_column(name)


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index f13973b..2b79e1f 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -133,10 +133,11 @@ def test_init_df_with_extra_col(test_pd_df):
 
 def test_init_df_with_meta(test_pd_df):
     # pass explicit meta dataframe with a scenario that doesn't exist in data
-    df = IamDataFrame(test_pd_df, meta=META_DF.iloc[[0, 2]][["foo"]])
+    df = IamDataFrame(test_pd_df, meta=META_DF[["foo"]])
 
     # check that scenario not existing in data is removed during initialization
     pd.testing.assert_frame_equal(df.meta, META_DF.iloc[[0, 1]])
+    assert df.scenario == ["scen_a", "scen_b"]
 
 
 def test_init_df_with_meta_incompatible_index(test_pd_df):
diff --git a/tests/test_iiasa.py b/tests/test_iiasa.py
index 29874bd..8478e09 100644
--- a/tests/test_iiasa.py
+++ b/tests/test_iiasa.py
@@ -224,10 +224,12 @@ def test_query_year(conn, test_df_year, kwargs):
 
     # test method via Connection
     df = conn.query(model="model_a", **kwargs)
+    assert df.model == ["model_a"]
     assert_iamframe_equal(df, exp.filter(**kwargs))
 
     # test top-level method
     df = read_iiasa(TEST_API, model="model_a", **kwargs)
+    assert df.model == ["model_a"]
     assert_iamframe_equal(df, exp.filter(**kwargs))