Datasets Reference
philanthropy.datasets
Synthetic data generators and real reference datasets for donor analytics.
load_ciob_fundraising()
Load the NYC CIOB "Official Fundraising by City Agencies" registry.
A real open-government dataset: every not-for-profit that a New York City
agency reported soliciting donations for, disclosed under the Conflicts of
Interest Board's (CIOB) mandate. It is an affiliation registry, one row
per (year, agency, nonprofit) link, not donor-level giving data.
It carries no gift amounts, donor records, or engagement labels, so it does
not support the RFM / propensity modelling in the rest of this library;
use :func:generate_synthetic_donor_data for that. It is included for
honest, reproducible analysis of agency ↔ nonprofit fundraising
relationships (e.g. most-solicited nonprofits, agency fundraising breadth).
Returns:
| Type | Description |
|---|---|
pandas.DataFrame of shape (2336, 3)
|
Columns:
|
Notes
Source: NYC Open Data, dataset basd-2jwn (Conflicts of Interest Board),
https://data.cityofnewyork.us/City-Government/Official-Fundraising-by-City-Agencies/basd-2jwn.
Distributed under the NYC Open Data Terms of Use (free public use). The CSV
is vendored with the package, so this loader needs no network access.
Source code in philanthropy/datasets/_ciob.py
load_donorschoose(path)
Load a user-downloaded DonorsChoose Donations file (ICPSR 37898, DS0001).
DonorsChoose Open Data, United States (ICPSR 37898,
doi:10.3886/ICPSR37898.v1) is distributed under its own terms of use
(TermsOfUse.html in the study download): research or statistical
purposes only, no investigation of specific research subjects, and no
redistribution without ICPSR's written agreement. This reader never
downloads, caches, or redistributes the file: point it at the delimited
37898-0001-Data.tsv (or .dta) you have already obtained yourself
under your own ICPSR account. Never commit rows read by this function,
or anything derived at the row level, to this repository or its docs;
aggregate statistics only.
CREATED_MONTH is recorded at month resolution (YYYY-MM), not
a full date; this function parses it to the first day of that month, so
any date-based feature computed from the result (recency, fiscal-year
boundaries, ...) inherits that one-month granularity rather than the
daily resolution the synthetic and KDD98 data provide.
This reader applies no filtering: AMOUNT rows at or below zero exist
in the source data and are passed through unchanged, every donor_type
("citizen donor", "organization", "teacher") is returned, and a
donation's own payment-source flags are returned as booleans rather than
acted on. Decide and document your own filters at the point you use this
data (e.g. the benchmark script), rather than relying on this function to
have applied them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the DS0001 Donations file, tab-separated ( |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
Notes
Source: DonorsChoose Open Data, United States, 2002-2019 (ICPSR 37898), https://www.icpsr.umich.edu/web/ICPSR/studies/37898. Cite as doi:10.3886/ICPSR37898.v1.
Source code in philanthropy/datasets/_donorschoose.py
generate_synthetic_donor_data(n_samples=1000, random_state=None)
Generate a realistic synthetic donor DataFrame for modelling and testing.
The returned dataset simulates a hospital's major-gifts prospect pool. Features are correlated in a domain-meaningful way:
- Donors with more
years_activeand higherevent_attendance_counthave a monotonically increasing probability of being labelled as a major donor (is_major_donor = 1). total_gift_amountis log-normally distributed and positively correlated withis_major_donor.last_gift_dateis sampled uniformly across the past five calendar years, with major donors skewed toward more recent activity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of synthetic donor records to generate. |
1000
|
random_state
|
int or None
|
Seed for the NumPy random-number generator. Pass an integer to
obtain a reproducible dataset; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
pd.DataFrame of shape (n_samples, 5)
|
A DataFrame with the following columns:
|
Examples:
>>> from philanthropy.datasets import generate_synthetic_donor_data
>>> df = generate_synthetic_donor_data(n_samples=500, random_state=42)
>>> df.shape
(500, 5)
>>> df.dtypes["is_major_donor"]
dtype('int64')
>>> bool(df["is_major_donor"].isin([0, 1]).all())
True
Notes
A latent giving capacity drives everything. It is a linear function of
years_active, event_attendance_count and an unobserved wealth term,
on a log-dollar scale. total_gift_amount is then drawn as a noisy
realisation of that capacity, and is_major_donor as a soft threshold on
it at :data:MAJOR_GIFT_CAPACITY. last_gift_date follows engagement.
The ordering matters. Capacity is a confounder that causes both the
giving history and the label, so total_gift_amount is a legitimate
predictor: informative, and limited by how well giving reveals capacity.
Before version 0.7.0 the label was drawn first and total_gift_amount
was drawn conditional on the label. That inverted the domain's causal
arrow, and it was measurable: a model given total_gift_amount beat the
Bayes rate of the generator's own process by roughly 19 ROC-AUC points,
which no model can legitimately do. Using cumulative lifetime giving to
predict "is a major donor" is also the classic fundraising leakage this
library exists to prevent, so the reference dataset was teaching the
anti-pattern. last_gift_date was a second target-derived feature for
the same reason.
The label remains statistically learnable and is not trivially predictable: held out on 4,000 rows, the documented feature set reaches ROC-AUC 0.814 and accuracy 0.759 against a Bayes accuracy ceiling of 0.806 given latent capacity. Sitting below that ceiling is the point.
The function never raises an error for valid inputs. Passing
n_samples=0 returns an empty DataFrame with the correct column
schema.
Source code in philanthropy/datasets/_generator.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 | |
make_donor_dataset(n_donors=200, fiscal_year_start=7, start_year=2018, end_year=2024, lapse_rate=0.25, major_gift_threshold=10000.0, random_state=42)
Generate a synthetic, seeded gift-level donor dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_donors
|
int
|
Number of synthetic donors to generate. |
200
|
fiscal_year_start
|
int
|
Reserved. Accepted for signature stability but not currently used when generating gift dates. |
7
|
start_year
|
int
|
Earliest calendar year used when sampling gift dates. |
2018
|
end_year
|
int
|
Latest calendar year used when sampling gift dates. |
2024
|
lapse_rate
|
float
|
Reserved. Accepted for signature stability but not currently used when generating gift dates. |
0.25
|
major_gift_threshold
|
float
|
Gift amount (inclusive) that marks a row as a major gift. |
10_000.0
|
random_state
|
int or None
|
Seed for the NumPy random-number generator. Pass an integer to
obtain a reproducible dataset; |
42
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
A gift-level DataFrame, not a donor-level DataFrame: each donor
contributes 1-5 gift rows, so
|
Examples:
>>> from philanthropy.datasets import make_donor_dataset
>>> df = make_donor_dataset(n_donors=5, random_state=0)
>>> df.shape
(15, 5)
>>> list(df.columns)
['donor_id', 'gift_date', 'gift_amount', 'appeal_code', 'is_major_gift']
>>> df['donor_id'].nunique()
5
Source code in philanthropy/datasets/_generator.py
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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
fetch_kdd98_donors(*, data_home=None, download_if_missing=True)
Fetch the KDD Cup 1998 direct-mail donor learning set.
A real donor-level dataset: 95,412 individuals who gave at least once
between June 1995 and June 1996, one row per donor, with a full
per-promotion mail and response history (ADATE_2..ADATE_24,
RDATE_2..RDATE_24, RAMNT_2..RAMNT_24) plus the outcome of
the 1997 mailing being predicted (TARGET_B, TARGET_D). That date
history is what makes as-of feature construction testable on it rather
than only on synthetic data; see :func:generate_synthetic_donor_data for
the synthetic equivalent used elsewhere in this library. Most date columns
are encoded YYMM (year, month) rather than as a datetime dtype.
This is a read-only public research dataset, not your data. Nothing
about your own donors, gifts, or environment is ever sent anywhere; the
only network traffic this function makes is fetching the dataset file
itself, once, to a local cache. It is never called automatically: no
other function in this library imports it or calls it during fit or
transform.
Under the dataset's terms of use, teaching or training material that uses it must not name the sponsoring organisation; cite it only as "KDD Cup 1998". This docstring follows that condition, and so should anything you write based on it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_home
|
str
|
Directory to cache the downloaded archive in. Defaults to the
|
None
|
download_if_missing
|
bool
|
If the archive is not already cached, download it. If False and the
archive is not cached, raise |
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame of shape (95412, 481)
|
One row per donor, columns as documented in the data dictionary below. Column dtypes are pandas' own inference over the raw CSV; this function does not recode or impute any of them. |
Raises:
| Type | Description |
|---|---|
OSError
|
If the archive is not cached and |
Notes
Source: the UCI KDD Archive,
https://kdd.ics.uci.edu/databases/kddcup98/kddcup98.html. Field-by-field
documentation: cup98dic.txt at the same location. Distributed for
general research and educational use under the terms stated on that page,
including the sponsor-naming restriction noted above and a request to
notify the dataset's contacts of any published results.
The archive is not vendored with this package (its terms require an
unmodified, individually-fetched copy); this function downloads it to a
local cache on first use, the way sklearn.datasets.fetch_* functions
do, and every later call reads the cached copy.
Source code in philanthropy/datasets/_kdd98.py
fetch_kdd98_val_donors(*, data_home=None, download_if_missing=True)
Fetch KDD Cup 1998's own held-out VALIDATION set, merged with its answer key.
fetch_kdd98_donors returns the learning file, the one every model in
this library fits and splits (55/15/30, see
scripts/benchmark_models_vs_baselines.py). This function returns the
competition's separate validation file (cup98VAL.txt): 96,367 more
donors that were never part of the learning file and never touched
during a learning-file split of any kind. TARGET_B/TARGET_D
(whether and how much each of them gave to the 97NK mailing) were
withheld from participants during the original competition and released
afterwards in a separate answer key, valtargt.txt; this function
downloads both and joins them on CONTROLN so the result has the same
shape as :func:fetch_kdd98_donors, including TARGET_B/TARGET_D.
Use this only to score a model already fit elsewhere (e.g. on the learning file), never to fit one: a validation file used for fitting stops being held out.
This is a read-only public research dataset, not your data, under the
same terms and the same sponsor-naming restriction as
:func:fetch_kdd98_donors; see that function's docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_home
|
str
|
Directory to cache the downloaded archives in. Defaults to the
|
None
|
download_if_missing
|
bool
|
If either archive is not already cached, download it. If False and
either is missing, raise |
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame of shape (96367, 481)
|
One row per donor, the same columns as :func: |
Raises:
| Type | Description |
|---|---|
OSError
|
If an archive is not cached and |
Notes
Source: the UCI KDD Archive,
https://kdd.ics.uci.edu/databases/kddcup98/kddcup98.html, the same page
and terms as the learning file. valtargt.readme at the same location
documents the answer-key file's three columns (CONTROLN,
TARGET_B, TARGET_D).
Source code in philanthropy/datasets/_kdd98.py
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 | |
load_karlan_list(path)
Load the Karlan and List matching-grant experiment from a local file.
Karlan, D. and List, J. A. (2007), "Does Price Matter in Charitable Giving? Evidence from a Large-Scale Natural Field Experiment", American Economic Review 97(5): 1774-1793. About 50,000 prior donors to one US nonprofit were mailed a fundraising letter in 2005; two in three, chosen at random, were offered a matching grant, with the match ratio, the cap on the matching pool and the example ask amount each randomised.
The replication package is openICPSR 113224 (doi:10.3886/E113224V1).
Its LICENSE.txt puts the data under CC BY 4.0 and the code under
BSD-3, copyright American Economic Association 2007, so anything built
on it must credit the authors and the AEA. This reader never downloads
the file: point it at AERtables1-5.dta from your own download.
Rows with every variable missing (47 in the published file) are dropped; nothing else is filtered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per donor:
|
Examples:
>>> from philanthropy.datasets import load_karlan_list
>>> df = load_karlan_list("AERtables1-5.dta")
>>> round(df["gave"].mean(), 3)
0.021
Source code in philanthropy/datasets/_karlan_list.py
make_donor_panel(n_donors=3000, n_years=7, start_fiscal_year=2018, include_encounters=False, random_state=None)
Generate a seeded multi-year donor panel with gift-level rows.
Unlike :func:generate_synthetic_donor_data, which returns one
already-aggregated row per donor, this returns the raw tables a
fundraising shop actually exports: a gift log, a donor table, and
optionally a clinical-encounter table. Aggregating them is left to the
caller, on purpose, because when you aggregate is the whole subject of
:doc:the leakage tutorial </tutorials/avoiding_temporal_data_leakage>.
Fiscal years run 1 July to 30 June and are labelled by the calendar year in which they end, so fiscal year 2019 spans 2018-07-01 to 2019-06-30. A donor gives at most once per fiscal year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_donors
|
int
|
Number of donors in the panel. Every donor appears in |
3000
|
n_years
|
int
|
Number of consecutive fiscal years. Deriving a "gave in the following year" label costs the last one, so the default yields six usable panel years. |
7
|
start_fiscal_year
|
int
|
Label of the first fiscal year. |
2018
|
include_encounters
|
bool
|
Also return an |
False
|
random_state
|
int or None
|
Seed for the NumPy generator. Pass an integer for a reproducible
panel; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
panel |
dict of str to pandas.DataFrame
|
|
Notes
There is no label column, deliberately. A label is a claim about a point in time, and shipping one pre-computed would hand every user the exact mistake this package exists to prevent. Derive it, as of the year you are scoring:
from philanthropy.datasets import make_donor_panel panel = make_donor_panel(n_donors=200, random_state=0) gifts = panel["gifts"] gave = set(zip(gifts["donor_id"], gifts["fiscal_year"])) label = int((7, 2020) in gave) # did donor 7 give in FY2020?
Every number here is invented. The panel establishes mechanisms and shapes,
never magnitudes you should quote for your own program; for magnitudes
measured on a real donor file, see
:doc:/explanation/real_data_replication.
Examples:
>>> from philanthropy.datasets import make_donor_panel
>>> panel = make_donor_panel(n_donors=500, random_state=42)
>>> sorted(panel)
['donors', 'gifts']
>>> list(panel["gifts"].columns)
['donor_id', 'gift_date', 'gift_amount', 'fiscal_year', 'appeal']
>>> int(panel["gifts"]["fiscal_year"].min()), int(panel["gifts"]["fiscal_year"].max())
(2018, 2024)
>>> len(panel["donors"])
500
Straight into RFM features, which the cross-sectional generator cannot do:
>>> from philanthropy.preprocessing import RFMTransformer
>>> rfm = RFMTransformer().fit_transform(panel["gifts"])
>>> list(rfm.columns)
['donor_id', 'recency', 'frequency', 'monetary']
With encounters, for the grateful-patient path:
>>> panel = make_donor_panel(
... n_donors=500, include_encounters=True, random_state=42
... )
>>> list(panel["encounters"].columns)
['donor_id', 'admit_date', 'discharge_date', 'service_line']
Source code in philanthropy/datasets/_panel.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
load_psid_philanthropy(data_path, do_path)
Load a user-downloaded PSID individual-level cross-year extract into a long household-giving/volunteering table.
The Panel Study of Income Dynamics (PSID) is a public-use survey; its
Conditions of Use (agreed to when registering at
https://simba.isr.umich.edu) require: no transfer of downloaded data to
a third party, no attempt to identify any individual or family, deletion
of the data after the analysis it was obtained for, reporting
publications that use it, and acknowledging PSID as the source. This
reader never downloads, caches, or redistributes PSID data: point it at
a fixed-width extract and its accompanying Stata .do file (column
positions only; no Stata or any other new dependency is used to read
it) that you obtained yourself from the PSID Data Center under your own
registration. Never commit PSID rows, or anything derived at the
row/person level, to this repository or its docs; aggregate statistics
only.
This reader is built against a fixed list of PSID ER/S variables (read
from PSID Data Center job 365729's own .do file and cross-checked
against its codebook, covering 2001 to 2023 "Current Year Heads
Individual Data": one row per person who was Head/Reference Person in
at least one selected wave). ER/S variable numbers are fixed, study-wide
PSID identifiers, not job-specific, so a differently-selected extract
that still includes these variables reads identically. An extract with
a smaller variable selection than job 365729 simply omits some
columns: a wave whose Head/Reference-person variable is entirely
absent is skipped outright, while a wave whose Head/Reference-person
variable is present but another of its variables is missing raises a
plain KeyError, since that combination means this table and the
extract disagree about what was selected.
For each wave, a person counts as that household's Head only when their
relation-to-head/reference-person code is 10; sequence number is not
used; the household key is the person's own 1968 interview number and
person number (ER30001 * 1000 + ER30002), which identifies them
(and so their household, while they are its Head) across every wave.
Giving amounts, itemized contributions, and volunteering hours all use
PSID's standard missing-data convention for a field of width w: a
value of w nines means Not Applicable/Refused and w nines with a
trailing 8 means Don't Know; both are recoded to NaN here rather
than left as large sentinel integers. family_income, wealth1,
and wealth2 are PSID-generated/imputed totals, not raw survey
responses, and were directly checked against the real data for this
convention: they carry no such sentinel values, so this reader leaves
them unrecoded (a large value there, e.g. a 7-digit income, is real).
total_giving sums whatever per-category amounts a wave actually
asked (NaN only when every category for that wave is NaN, so a
few Don't-Know categories do not blank the whole total); the categories
themselves differ by wave, most notably 2001 (five categories plus one
combined "checkpoint" amount for the rest) and 2021 onward (no
"community" category).
Volunteering hours are three different measures that must never be
combined into one series: 2001 asks hours volunteered last year,
split by head and spouse; 2003 and 2005 ask hours volunteered
"regularly," one battery of up to 14 slots (one per organization type)
that is not split by head/spouse at all, so this reader returns it as
a single household-level total; 2017, 2019, 2021 and 2023 ask hours
volunteered in a typical week, again split by head and spouse. 2007
through 2015 have no hours amount in this extract (only
whether-volunteered flags, which this reader does not return). The
three measures are returned as separate columns, NaN in every wave
they do not apply to, so a caller cannot accidentally splice them into
a single trend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str
|
Path to the fixed-width ASCII data file (PSID calls this the
|
required |
do_path
|
str
|
Path to the accompanying Stata |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Long format, one row per (household, wave actually observed for
that household's Head). Columns: |
Notes
Source: Panel Study of Income Dynamics, public use dataset, produced and distributed by the Survey Research Center, Institute for Social Research, University of Michigan, Ann Arbor, MI.
Source code in philanthropy/datasets/_psid.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |