nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: IAMconsortium/pyam
Pull request #66 (2018-06-25 13:28:58)

bugfix for `interpolate()` - duplication of values
This PR fixes a bug in the ` interpolate` function, where values are duplicated if they are already defined. The fix only interpolates (and adds to the `IamDataFrame.data` only years that are missing.

gidden: Hey @danielhuppmann, looks good to me. However, could you add a test that fails without this fix? Can you also add this to the release notes?
gidden: Probably easiest to rebase on master to get the release notes, FYI.
danielhuppmann: rebased, extended the test for the `interpolate()` function, added a release note

@gidden, should be good to go!

Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 2b32a51..f9bb396 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -1,2 +1,4 @@
 
 # Next Release
+
+- (#66)[https://github.com/IAMconsortium/pyam/pull/66] Fixes a bug in the `interpolate()` function ( duplication of data points if already defined)
diff --git a/pyam/core.py b/pyam/core.py
index 0755fcb..b6404a3 100644
--- a/pyam/core.py
+++ b/pyam/core.py
@@ -215,12 +215,15 @@ class IamDataFrame(object):
         """
         df = self.pivot_table(index=IAMC_IDX, columns=['year'],
                               values='value', aggfunc=np.sum)
+        # drop year-rows where values are already defined
+        if year in df.columns:
+            df = df[np.isnan(df[year])]
         fill_values = df.apply(fill_series,
                                raw=False, axis=1, year=year)
         fill_values = fill_values.dropna().reset_index()
         fill_values = fill_values.rename(columns={0: "value"})
         fill_values['year'] = year
-        self.data = self.data.append(fill_values)
+        self.data = self.data.append(fill_values, ignore_index=True)
 
     def as_pandas(self, with_metadata=False):
         """Return this as a pd.DataFrame


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index 7ad598e..c14980c 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -318,9 +318,14 @@ def test_interpolate(test_df):
            'years': [2005, 2007, 2010], 'value': [1, 3, 6]}
     exp = pd.DataFrame(dct).pivot_table(index=['model', 'scenario'],
                                         columns=['years'], values='value')
-    obs = test_df.filter(variable='Primary Energy').timeseries()
+    variable = {'variable': 'Primary Energy'}
+    obs = test_df.filter(**variable).timeseries()
     npt.assert_array_equal(obs, exp)
 
+    # redo the inpolation and check that no duplicates are added
+    test_df.interpolate(2007)
+    assert not test_df.filter(**variable).data.duplicated().any()
+
 
 def test_set_meta_as_named_series(meta_df):
     idx = pd.MultiIndex(levels=[['a_scenario'], ['a_model'], ['a_region']],
Repository: IAMconsortium/pyam
Pull request #78 (2018-08-09 10:28:48)

bugfix for using multiple axes in region plots
# Please confirm that this PR has done the following:

- [ ] Tests Added
- [ ] Documentation Added
- [ ] Description in RELEASE_NOTES.md Added

# 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
```


gidden: g2g @danielhuppmann 

Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 57b9131..8743eaa 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -1,6 +1,7 @@
 
 # Next Release
 
+- (#80)[https://github.com/IAMconsortium/pyam/pull/80] Extend the pseudo-regexp syntax for filtering in `pattern_match()`
 - (#73)[https://github.com/IAMconsortium/pyam/pull/73] Adds ability to remove labels for markers, colors, or linestyles
 - (#71)[https://github.com/IAMconsortium/pyam/pull/71] Line plots `legend` keyword can now be a dictionary of legend arguments
 - (#70)[https://github.com/IAMconsortium/pyam/pull/70] Support reading of both SSP and RCP data files downloaded from the IIASA database.
diff --git a/pyam/plotting.py b/pyam/plotting.py
index b8f9410..07cfeb1 100644
--- a/pyam/plotting.py
+++ b/pyam/plotting.py
@@ -207,7 +207,7 @@ def region_plot(df, column='value', ax=None, crs=None, gdf=None, add_features=Tr
                 fraction=0.022,  # these are magic numbers
                 pad=0.02,       # that just seem to "work"
             )
-        cb = plt.colorbar(scalar_map, **cbar)
+        plt.colorbar(scalar_map, ax=ax, **cbar)
 
     if legend is not False:
         if legend is True:  # use some defaults
diff --git a/pyam/utils.py b/pyam/utils.py
index fcb20f9..5834470 100644
--- a/pyam/utils.py
+++ b/pyam/utils.py
@@ -231,8 +231,11 @@ def pattern_match(data, values, level=None):
         if isstr(s):
             regexp = (str(s)
                       .replace('|', '\\|')
+                      .replace('.', '\.')  # `.` has to be replaced before `*`
                       .replace('*', '.*')
                       .replace('+', '\+')
+                      .replace('(', '\(')
+                      .replace(')', '\)')
                       ) + "$"
             pattern = re.compile(regexp)
             subset = filter(pattern.match, data)


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 3e683ec..4f70016 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -51,3 +51,24 @@ def test_pattern_match_plus():
     exp = [False, True, True, True]
 
     assert (obs == exp).all()
+
+
+def test_pattern_match_dot():
+    data = pd.Series(['foo', 'fo.'])
+    values = ['fo.']
+
+    obs = utils.pattern_match(data, values)
+    exp = [False, True]
+
+    assert (obs == exp).all()
+
+
+def test_pattern_match_brackets():
+    data = pd.Series(['foo (bar)', 'foo bar'])
+    values = ['foo (bar)']
+
+    obs = utils.pattern_match(data, values)
+    exp = [True, False]
+
+    print(obs)
+    assert (obs == exp).all()
Repository: IAMconsortium/pyam
Pull request #80 (2018-08-09 11:55:16)

escape brackets and dot in `pattern_match()`
# Please confirm that this PR has done the following:

- [x] Tests Added
- [x] Documentation Added - in line with current documentation
- [x] Description in RELEASE_NOTES.md Added

# Description

This PR extends the pseudo-syntax to escape regexp-reserved characters `.` and brackets when filtering an `IamDataFrame`.

See #79 for a nice-to-have feature to deactivate the pseudo-syntax.

gidden: lgtm!

Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 57b9131..8743eaa 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -1,6 +1,7 @@
 
 # Next Release
 
+- (#80)[https://github.com/IAMconsortium/pyam/pull/80] Extend the pseudo-regexp syntax for filtering in `pattern_match()`
 - (#73)[https://github.com/IAMconsortium/pyam/pull/73] Adds ability to remove labels for markers, colors, or linestyles
 - (#71)[https://github.com/IAMconsortium/pyam/pull/71] Line plots `legend` keyword can now be a dictionary of legend arguments
 - (#70)[https://github.com/IAMconsortium/pyam/pull/70] Support reading of both SSP and RCP data files downloaded from the IIASA database.
diff --git a/pyam/utils.py b/pyam/utils.py
index fcb20f9..5834470 100644
--- a/pyam/utils.py
+++ b/pyam/utils.py
@@ -231,8 +231,11 @@ def pattern_match(data, values, level=None):
         if isstr(s):
             regexp = (str(s)
                       .replace('|', '\\|')
+                      .replace('.', '\.')  # `.` has to be replaced before `*`
                       .replace('*', '.*')
                       .replace('+', '\+')
+                      .replace('(', '\(')
+                      .replace(')', '\)')
                       ) + "$"
             pattern = re.compile(regexp)
             subset = filter(pattern.match, data)


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 3e683ec..4f70016 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -51,3 +51,24 @@ def test_pattern_match_plus():
     exp = [False, True, True, True]
 
     assert (obs == exp).all()
+
+
+def test_pattern_match_dot():
+    data = pd.Series(['foo', 'fo.'])
+    values = ['fo.']
+
+    obs = utils.pattern_match(data, values)
+    exp = [False, True]
+
+    assert (obs == exp).all()
+
+
+def test_pattern_match_brackets():
+    data = pd.Series(['foo (bar)', 'foo bar'])
+    values = ['foo (bar)']
+
+    obs = utils.pattern_match(data, values)
+    exp = [True, False]
+
+    print(obs)
+    assert (obs == exp).all()
Repository: IAMconsortium/pyam
Pull request #151 (2018-12-05 15:07:57)

test that to_excel writes a file
Simple test additions to check the potential issue raised by @Chilipp in #149 

gidden: Hey @danielhuppmann, if I remember correctly, you develop mostly on mac. Could you take a look at this? Thanks again @Chillip for bringing it to our attention.
gidden: Should fix #149
gidden: Given the timeliness needed, will merge directly now.

Patch:
diff --git a/pyam/core.py b/pyam/core.py
index 98274da..8e0e8ba 100644
--- a/pyam/core.py
+++ b/pyam/core.py
@@ -799,10 +799,13 @@ class IamDataFrame(object):
         if (path is None and writer is None) or \
            (path is not None and writer is not None):
             raise ValueError('Only one of path and writer must have a value')
+        close = writer is None
         if writer is None:
             writer = pd.ExcelWriter(path)
         self._to_file_format().to_excel(writer, sheet_name=sheet_name,
                                         index=index, **kwargs)
+        if close:
+            writer.close()
 
     def export_metadata(self, path):
         """Export metadata to Excel


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index b4821b4..e68f54b 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -49,6 +49,20 @@ def test_init_df_from_timeseries(test_df):
     pd.testing.assert_frame_equal(df.timeseries(), test_df.timeseries())
 
 
+def test_to_excel(test_df):
+    fname = 'foo_testing.xlsx'
+    test_df.to_excel(fname)
+    assert os.path.exists(fname)
+    os.remove(fname)
+
+
+def test_to_csv(test_df):
+    fname = 'foo_testing.csv'
+    test_df.to_csv(fname)
+    assert os.path.exists(fname)
+    os.remove(fname)
+
+
 def test_get_item(test_df):
     assert test_df['model'].unique() == ['a_model']
 
diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py
index 74f0bb8..13b7b4b 100644
--- a/tests/test_tutorials.py
+++ b/tests/test_tutorials.py
@@ -63,6 +63,8 @@ def test_pyam_first_steps(capsys):
     fname = os.path.join(tut_path, 'pyam_first_steps.ipynb')
     nb, errors = _notebook_run(fname, capsys=capsys)
     assert errors == []
+    assert os.path.exists(os.path.join(tut_path, 'tutorial_export.xlsx'))
+    assert os.path.exists(os.path.join(tut_path, 'tutorial_metadata.xlsx'))
 
 
 @pytest.mark.skipif(not jupyter_installed, reason=jupyter_reason)