From eaafdb38af52c2a138aca6ee1b1fd24d8bd3c2de Mon Sep 17 00:00:00 2001 From: Zhiyi Wu Date: Tue, 13 Jul 2021 18:45:00 +0100 Subject: [PATCH] make alchemlyb.concat behave like pd.concat (PR #152) * fix #150 * `alchemlyb.concat([])` now raises ValueError (like pandas.concat()) * Update CHANGES * add test --- CHANGES | 2 ++ src/alchemlyb/__init__.py | 6 +++++- src/alchemlyb/tests/test_units.py | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 8d30f716..2ada18e1 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,8 @@ Enhancements Deprecations Fixes + - `alchemlyb.concat()` should raise ValueError when given empty list, to be compatible with + pandas.concat() (issue #150, PR #152). - Fix the support for pandas >= 1.3 (issue #147, PR #148). - Fix separate_dhdl not work for multiple columns (issue #149, PR #151). diff --git a/src/alchemlyb/__init__.py b/src/alchemlyb/__init__.py index 363a4f4c..061e2fe2 100644 --- a/src/alchemlyb/__init__.py +++ b/src/alchemlyb/__init__.py @@ -47,7 +47,11 @@ def concat(objs, *args, **kwargs): .. versionadded:: 0.5.0''' # Sanity check - attrs = objs[0].attrs + try: + attrs = objs[0].attrs + except IndexError: # except empty list as input + raise ValueError('No objects to concatenate') + for obj in objs: if attrs != obj.attrs: raise ValueError('All pandas objects should have the same attrs.') diff --git a/src/alchemlyb/tests/test_units.py b/src/alchemlyb/tests/test_units.py index 19014656..6e9ee343 100644 --- a/src/alchemlyb/tests/test_units.py +++ b/src/alchemlyb/tests/test_units.py @@ -33,6 +33,11 @@ def test_concat(): with pytest.raises(ValueError): alchemlyb.concat([df1, df2]) +def test_concat_empty(): + '''Test if empty raise the right error.''' + with pytest.raises(ValueError): + alchemlyb.concat([]) + def test_setT(): '''Test setting temperature.''' df = pd.DataFrame(data={'col1': [1, 2]})