diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6184366..2fb35c5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,7 +18,7 @@ This package uses [`poetry`](https://python-poetry.org/docs/master/#installation 1. Install `poetry` ```sh -$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python - +$ curl -sSL https://install.python-poetry.org | python3 - ``` 2. Install all project dependencies (including development/optional dependencies). diff --git a/redcap/methods/base.py b/redcap/methods/base.py index fa95baa..7dc1d3a 100644 --- a/redcap/methods/base.py +++ b/redcap/methods/base.py @@ -381,20 +381,18 @@ def _return_data( return response if not df_kwargs: - if record_type == "eav": - df_kwargs = {} - elif content == "exportFieldNames": - df_kwargs = {"index_col": "original_field_name"} + df_kwargs = {} + + if "index_col" not in df_kwargs.keys() and record_type != "eav": + if content == "exportFieldNames": + df_kwargs["index_col"] = "original_field_name" elif content == "metadata": - df_kwargs = {"index_col": "field_name"} + df_kwargs["index_col"] = "field_name" elif content in ["report", "record"]: if self.is_longitudinal: - df_kwargs = {"index_col": [self.def_field, "redcap_event_name"]} + df_kwargs["index_col"] = [self.def_field, "redcap_event_name"] else: - df_kwargs = {"index_col": self.def_field} - # catchall for other endpoints - else: - df_kwargs = {} + df_kwargs["index_col"] = self.def_field response = cast(str, response) diff --git a/tests/unit/test_simple_project.py b/tests/unit/test_simple_project.py index f9589dc..d1436b4 100644 --- a/tests/unit/test_simple_project.py +++ b/tests/unit/test_simple_project.py @@ -414,6 +414,41 @@ def test_df_export(simple_project): assert hasattr(dataframe.index, "name") +def test_df_export_empty_df_kwargs(simple_project): + dataframe = simple_project.export_records(format_type="df", df_kwargs={}) + + assert isinstance(dataframe, pd.DataFrame) + + # should be set by default + assert dataframe.index.name == "record_id" + + +def test_df_export_extend_df_kwargs_default_values(simple_project): + kept_columns = ["first_name", "study_id"] + + dataframe = simple_project.export_records( + format_type="df", df_kwargs={"usecols": kept_columns + ["record_id"]} + ) + + assert isinstance(dataframe, pd.DataFrame) + + # should be set by default + assert dataframe.index.name == "record_id" + + # expected based on usecols (else would have additional columns) + assert dataframe.columns.tolist() == kept_columns + + +def test_df_export_override_df_kwargs_default_values(simple_project): + dataframe = simple_project.export_records( + format_type="df", df_kwargs={"index_col": "first_name"} + ) + + assert isinstance(dataframe, pd.DataFrame) + + assert dataframe.index.name == "first_name" + + def test_export_with_date_filters(simple_project): all_records = simple_project.export_records() limited_records = simple_project.export_records(