Ingest Reference
philanthropy.ingest
On-ramps from an upstream donor system to a PhilanthroPy donor-level feature table.
UniSchema: read_constituent_events loads UniSchema's JSON / NDJSON egress
files; constituent_events_to_features aggregates them into the
one-row-per-donor feature frame the estimators consume.
CiviCRM: read_civicrm_contributions loads a contribution export CSV;
civicrm_contributions_to_features aggregates it the same way, dropping
test-mode and non-Completed rows first.
civicrm_contributions_to_features(contributions, *, reference_date=None, statuses=('Completed',))
Aggregate a CiviCRM contribution log into donor-level features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contributions
|
iterable of mapping, or DataFrame
|
CiviCRM contribution rows. Accepts the output of
:func: |
required |
reference_date
|
str or datetime - like
|
Anchor for the recency features ( |
None
|
statuses
|
sequence of str or None
|
Contribution statuses to count, matched case-insensitively against
|
``("Completed",)``
|
Returns:
| Name | Type | Description |
|---|---|---|
features |
DataFrame
|
One row per donor, indexed by |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If |
UserWarning
|
If the batch mixes currencies. |
Examples:
>>> rows = [
... {"Contact ID": "101", "Contribution Date": "2025-01-15",
... "Total Amount": "250.00", "Contribution Status": "Completed"},
... {"Contact ID": "101", "Contribution Date": "2025-06-01",
... "Total Amount": "1,000.00", "Contribution Status": "Completed"},
... {"Contact ID": "101", "Contribution Date": "2025-06-02",
... "Total Amount": "99.00", "Contribution Status": "Failed"},
... ]
>>> feats = civicrm_contributions_to_features(rows)
>>> float(feats.loc["101", "total_gift_amount"])
1250.0
>>> int(feats.loc["101", "gift_count"])
2
Source code in philanthropy/ingest/_civicrm.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
read_civicrm_contributions(path)
Read CiviCRM contribution export CSV(s) into one normalised frame.
Accepts either a single .csv file or a directory, which is walked
recursively and whose *.csv files are concatenated in sorted
relative-path order, the shape you get from keeping a folder of monthly
exports. Symlinks are not followed.
Every column is read as text and the headers are normalised to the APIv4
spelling, so "Total Amount", "Contact ID" and "Contribution Date"
arrive as total_amount, contact_id and receive_date. Nothing else
is done to the rows: test-mode and non-Completed contributions are
still present, and it is
:func:civicrm_contributions_to_features that drops them and types the
values. Reading is deliberately lossless so the raw export stays inspectable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
CSV file, or a directory of them. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
contributions |
DataFrame
|
The export as written, with normalised column names and text values. A directory holding no CSV returns an empty frame. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
Source code in philanthropy/ingest/_civicrm.py
constituent_events_to_features(events, *, reference_date=None, deduplicate=True)
Aggregate a UniSchema ConstituentEvent stream into donor features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
iterable of mapping, or DataFrame
|
Records following UniSchema's |
required |
reference_date
|
str or datetime - like
|
Anchor for the recency features ( |
None
|
deduplicate
|
bool
|
Drop repeated |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
features |
DataFrame
|
One row per constituent, indexed by |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If the batch mixes currencies (more than one distinct |
Examples:
>>> events = [
... {"constituentEmail": "a@x.edu", "eventType": "DONATION",
... "sourceSystem": "GIVECAMPUS", "amount": 250.0,
... "createdAt": "2025-03-01T12:00:00Z"},
... {"constituentEmail": "a@x.edu", "eventType": "EVENT_REGISTRATION",
... "sourceSystem": "CVENT", "createdAt": "2025-06-01T09:00:00Z"},
... ]
>>> feats = constituent_events_to_features(events)
>>> float(feats.loc["a@x.edu", "total_gift_amount"])
250.0
>>> int(feats.loc["a@x.edu", "event_attendance_count"])
1
Source code in philanthropy/ingest/_constituent_events.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
read_constituent_events(path)
Read UniSchema egress files into a list of ConstituentEvent dicts.
Handles the shapes UniSchema's egress writes:
- a single
.jsonfile holding one event (object) or many (array); - a
.ndjson/.jsonlbatch, one event per line; - a directory, which is walked recursively: every
*.json,*.ndjson, and*.jsonlfile at any depth is read and concatenated, sorted by relative path. This handles UniSchema's date-partitioned egress ({prefix}/{vendor}/{yyyy}/{mm}/{dd}/{eventId}.json); a flat directory still works too.*.manifest.jsonbatch sidecars are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
File or directory to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
events |
list of dict
|
Parsed events, ready to pass to :func: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |