-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize an
IamDataFrame
from pd.DataFrame
with formatting specs (
#199) * initial impl for giving default pyam dataframes * move again into core * small fixes * update concat to try to cast to dataframe * appease stickler * fix `TypeError` in `concat()` * add `concat_with_pipe()` in `utils` with some tests * cast elements in `concat_with_pipe()` to `str` * drop `nan` instead of casting to `str` * insert function `reduce_hierarchy()` and add tests * raise error if initializing `IamDataFrame`with duplicate rows in `data` * remove unused legacy function * add auxiliary function `sort_data()` for consistent ordering of cols * use `sort_data()` in `append()` * add `sort_data()` to some unit tests to make them pass * refactor and add first test for `df_to_pyam()` * clean-up before stickler gives me an earful... * clean-up of implementation of `df_to_pyam()` * update docstrings * allow input-df with columns `year` and `time` * defining `defaults` doesn't seem to be necessary * require `value` arg in `df_to_pyam()` instead of using all columns * check for conflicts with existing columns, add test * add option in `df_to_pyam()` to rename cols * add feature to concat required column from multiple given columns * make `cast_to_iam` partof `format_data()` * rename function to `read_file()` because it can only read one file * pass kwargs for `format_data` through `read_file()` * pep8 and docstring cleanup * add to release notes * minor edits as requested by @gidden in the review
- Loading branch information
1 parent
5a6992d
commit fcb81fe
Showing
6 changed files
with
203 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
import pandas as pd | ||
from pyam import IamDataFrame, compare | ||
|
||
|
||
def test_cast_from_value_col(meta_df): | ||
df_with_value_cols = pd.DataFrame([ | ||
['model_a', 'scen_a', 'World', 'EJ/y', 2005, 1, 0.5], | ||
['model_a', 'scen_a', 'World', 'EJ/y', 2010, 6., 3], | ||
['model_a', 'scen_b', 'World', 'EJ/y', 2005, 2, None], | ||
['model_a', 'scen_b', 'World', 'EJ/y', 2010, 7, None] | ||
], | ||
columns=['model', 'scenario', 'region', 'unit', 'year', | ||
'Primary Energy', 'Primary Energy|Coal'], | ||
) | ||
df = IamDataFrame(df_with_value_cols, | ||
value=['Primary Energy', 'Primary Energy|Coal']) | ||
|
||
assert compare(meta_df, df).empty | ||
pd.testing.assert_frame_equal(df.data, meta_df.data) | ||
|
||
|
||
def test_cast_with_model_arg_raises(): | ||
df = pd.DataFrame([ | ||
['model_a', 'scen_a', 'World', 'EJ/y', 2005, 1, 0.5], | ||
], | ||
columns=['model', 'scenario', 'region', 'unit', 'year', | ||
'Primary Energy', 'Primary Energy|Coal'], | ||
) | ||
pytest.raises(ValueError, IamDataFrame, df, model='foo') | ||
|
||
|
||
def test_cast_with_model_arg(meta_df): | ||
df = meta_df.timeseries().reset_index() | ||
df.rename(columns={'model': 'foo'}, inplace=True) | ||
|
||
df = IamDataFrame(df, model='foo') | ||
assert compare(meta_df, df).empty | ||
pd.testing.assert_frame_equal(df.data, meta_df.data) | ||
|
||
|
||
def test_cast_by_column_concat(meta_df): | ||
df = pd.DataFrame([ | ||
['scen_a', 'World', 'Primary Energy', None, 'EJ/y', 1, 6.], | ||
['scen_a', 'World', 'Primary Energy', 'Coal', 'EJ/y', 0.5, 3], | ||
['scen_b', 'World', 'Primary Energy', None, 'EJ/y', 2, 7], | ||
], | ||
columns=['scenario', 'region', 'var_1', 'var_2', 'unit', 2005, 2010], | ||
) | ||
|
||
df = IamDataFrame(df, model='model_a', variable=['var_1', 'var_2']) | ||
assert compare(meta_df, df).empty | ||
pd.testing.assert_frame_equal(df.data, meta_df.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.