Skip to main content

Use pandas with clinicedc/edc projects

Project description

pypi actions codecov downloads

edc-pdutils

Use pandas with the Edc

Using the management command to export to CSV and STATA

To export as CSV where the delimiter is |

python manage.py export_models -a ambition_subject -p /ambition/export

To export as STATA dta:

python manage.py export_models -a ambition_subject -f stata -p /ambition/export

To export encrypted fields as well:

python manage.py export_models -a ambition_subject -f stata -p /ambition/export  --decrypt

To export using a simpler filename that drops the tablename app_label prefix and does not include a datestamp suffix:

python manage.py export_models -a ambition_subject -f stata -p /ambition/export  --use_simple_filename

Export manually

To export Crf data, for example:

from edc_pdutils.df_exporters import CsvCrfTablesExporter
from edc_pdutils.df_handlers import CrfDfHandler

app_label = 'ambition_subject'
csv_path = '/Users/erikvw/Documents/ambition/export/'
date_format = '%Y-%m-%d'
sep = '|'
exclude_history_tables = True

class MyDfHandler(CrfDfHandler):
    visit_tbl = f'{app_label}_subjectvisit'
    exclude_columns = ['form_as_json', 'survival_status','last_alive_date',
                       'screening_age_in_years', 'registration_datetime',
                       'subject_type']

class MyCsvCrfTablesExporter(CsvCrfTablesExporter):
    visit_column = 'subject_visit_id'
    datetime_fields = ['randomization_datetime']
    df_handler_cls = MyDfHandler
    app_label = app_label
    export_folder = csv_path

sys.stdout.write('\n')
exporter = MyCsvCrfTablesExporter(
    export_folder=csv_path,
    exclude_history_tables=exclude_history_tables
)
exporter.to_csv(date_format=date_format, delimiter=sep)

To export INLINE data for any CRF configured with an inline, for example:

class MyDfHandler(CrfDfHandler):
    visit_tbl = 'ambition_subject_subjectvisit'
    exclude_columns = ['form_as_json', 'survival_status','last_alive_date',
                       'screening_age_in_years', 'registration_datetime',
                       'subject_type']


class MyCsvCrfInlineTablesExporter(CsvCrfInlineTablesExporter):
    visit_columns = ['subject_visit_id']
    df_handler_cls = MyDfHandler
    app_label = 'ambition_subject'
    export_folder = csv_path
    exclude_inline_tables = [
        'ambition_subject_radiology_abnormal_results_reason',
        'ambition_subject_radiology_cxr_type']
sys.stdout.write('\n')
exporter = MyCsvCrfInlineTablesExporter()
exporter.to_csv(date_format=date_format, delimiter=sep)

Using model_to_dataframe

from edc_pdutils.model_to_dataframe import ModelToDataframe
from edc_pdutils.utils import get_model_names
from edc_pdutils.df_exporters.csv_exporter import CsvExporter

app_label = 'ambition_subject'
csv_path = '/Users/erikvw/Documents/ambition/export/'
date_format = '%Y-%m-%d'
sep = '|'

for model_name in get_model_names(
        app_label=app_label,
        # with_columns=with_columns,
        # without_columns=without_columns,
    ):
    m = ModelToDataframe(model=model_name)
    exporter = CsvExporter(
        data_label=model_name,
        date_format=date_format,
        delimiter=sep,
        export_folder=csv_path,
    )
    exported = exporter.to_csv(dataframe=m.dataframe)

Settings

EXPORT_FILENAME_TIMESTAMP_FORMAT: True/False (Default: False)

By default a timestamp of the current date is added as a suffix to CSV export filenames.

By default a timestamp of format %Y%m%d%H%M%S is added.

EXPORT_FILENAME_TIMESTAMP_FORMAT may be set to an empty string or a valid format for strftime.

If EXPORT_FILENAME_TIMESTAMP_FORMAT is set to an empty string, “”, a suffix is not added.

For example:

# default
registered_subject_20190203112555.csv

# EXPORT_FILENAME_TIMESTAMP_FORMAT = "%Y%m%d"
registered_subject_20190203.csv

# EXPORT_FILENAME_TIMESTAMP_FORMAT = ""
registered_subject.csv

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edc-pdutils-0.3.17.tar.gz (55.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

edc_pdutils-0.3.17-py3-none-any.whl (69.6 kB view details)

Uploaded Python 3

File details

Details for the file edc-pdutils-0.3.17.tar.gz.

File metadata

  • Download URL: edc-pdutils-0.3.17.tar.gz
  • Upload date:
  • Size: 55.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for edc-pdutils-0.3.17.tar.gz
Algorithm Hash digest
SHA256 02e5c75363f506493c4caa79772a23177b129937c3164a58859397d8b014ee0a
MD5 aeb3fd526dd2f76858f6f67dbfd1ebab
BLAKE2b-256 850e072bccc7d38bd7a63e06e50066967af87f949ed3237f50dfa16be59d23a9

See more details on using hashes here.

File details

Details for the file edc_pdutils-0.3.17-py3-none-any.whl.

File metadata

  • Download URL: edc_pdutils-0.3.17-py3-none-any.whl
  • Upload date:
  • Size: 69.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for edc_pdutils-0.3.17-py3-none-any.whl
Algorithm Hash digest
SHA256 4e7ae24ca8d80a10817eede13d8c019b214c63cf8618ac5012b6990a953ca19d
MD5 3164348e3bdde066ab9ad48015b86bec
BLAKE2b-256 c7b67c9c9e8f5739feb6f53854537028cbdfb28db455b0461e1509d54bdfece9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page