Skip to content

Commit

Permalink
✔️ Keep default values in df_kwargs (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugGit authored Jul 5, 2023
1 parent 83c2826 commit a168006
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
18 changes: 8 additions & 10 deletions redcap/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_simple_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a168006

Please sign in to comment.