/
ddi.py
391 lines (351 loc) · 14 KB
/
ddi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
# This file is part of ipumspy.
# For copyright and licensing information, see the NOTICE and LICENSE files
# in this project's top-level directory, and also on-line at:
# https://github.com/ipums/ipumspy
"""
Utilities for working with IPUMS DDI formats
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Union
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import Element
import numpy as np
import pandas as pd
@dataclass(frozen=True)
class VariableDescription:
"""
Individual variables are described in the DDI. These are representations
of those descriptions as dataclasses.
"""
# pylint: disable=too-many-instance-attributes
id: str # pylint: disable=invalid-name
"""variable id (this is the same as its name)"""
name: str
"""variable name"""
rectype: str
"""record type"""
codes: Dict[str, Union[int, str]]
"""a dictionary of codes and value labels"""
start: int
"""variable's starting column in the extract data file"""
end: int
"""variable's final column in the extract data file"""
label: str
"""variable label"""
description: str
"""variable description"""
concept: str
"""IPUMS variable group"""
vartype: str
"""variable data type"""
notes: str
"""notes about this variable from the ddi"""
shift: Optional[int]
"""number of implied decimal places"""
@property
def python_type(self) -> type:
"""
The Python type of this variable.
"""
if self.vartype == "numeric":
if (self.shift is None) or (self.shift == 0):
return int
return float
return str
@property
def numpy_type(self) -> type:
"""
The Numpy type of this variable. Note that this type must support nullability,
and hence even for integers it is "float64".
"""
if self.vartype == "numeric":
if (self.shift is None) or (self.shift == 0):
return np.float64
return np.float64
return str
@property
def pandas_type(self) -> type:
"""
The Pandas type of this variable. This supports the recently added nullable
pandas dtypes, and so the integer type is "Int64" and the string type is
"string" (instead of "object")
"""
if self.vartype == "numeric":
if (self.shift is None) or (self.shift == 0):
return pd.Int64Dtype()
return np.float64
return pd.StringDtype()
@property
def pandas_type_efficient(self) -> type:
"""
In contrary to `self.pandas_type`, `self.pandas_type_efficient` doesn't implement "Int64" type but "numpy.float64" for
integer type. It's more efficient and pandas uses this approach for type inference:
https://pandas-docs.github.io/pandas-docs-travis/user_guide/integer_na.html
It can be considered as a mix between `self.pandas_type` and `self.numpy_type`
"""
if self.vartype == "numeric":
if (self.shift is None) or (self.shift == 0):
return np.float64
return np.float64
return pd.StringDtype()
@classmethod
def read(cls, elt: Element, ddi_namespace: str) -> VariableDescription:
"""
Read an XML description of a variable.
Args:
elt: xml element tree from parsed extract ddi
ddi_namespace: ddi namespace that says what the xmlns is for the file
Returns:
VariableDescription object
"""
namespaces = {"ddi": ddi_namespace}
vartype = elt.find("./ddi:varFormat", namespaces).attrib["type"]
labels_dict = {}
for cat in elt.findall("./ddi:catgry", namespaces):
label = cat.find("./ddi:labl", namespaces).text
value = cat.find("./ddi:catValu", namespaces).text
# make values integers when possible
if vartype == "numeric":
labels_dict[label] = int(value)
else:
labels_dict[label] = value
# rectype attribute only exists for hierarchical extracts
try:
var_rectype = elt.attrib["rectype"]
# stick an empty string in this attribute for rectangular extracts
except KeyError:
var_rectype = ""
# if a variable has notes, capture those
try:
var_notes = elt.find("./ddi:notes", namespaces).text
except AttributeError:
var_notes = ""
return cls(
id=elt.attrib["ID"],
name=elt.attrib["name"],
rectype=var_rectype,
codes=labels_dict,
start=int(elt.find("./ddi:location", namespaces).attrib["StartPos"])
- 1, # 0 based in python
end=int(
elt.find("./ddi:location", namespaces).attrib["EndPos"]
), # Exclusive ends in python
label=elt.find("./ddi:labl", namespaces).text,
description=elt.find("./ddi:txt", namespaces).text,
concept=elt.find("./ddi:concept", namespaces).text,
vartype=vartype,
notes=var_notes,
shift=int(elt.attrib.get("dcml")) if "dcml" in elt.attrib else None,
)
@dataclass(frozen=True)
class FileDescription:
"""
In the IPUMS DDI, the file has its own particular description. Extract
that from the XML.
"""
filename: str
"""IPUMS extract ddi file name"""
description: str
"""IPUMS ddi file description"""
structure: Literal["rectangular", "hierarchical"]
"""
IPUMS extract data file structure.
"""
rectypes: List[str]
"""
Record types included in the IPUMS extract.
This is an empty list for rectangular extracts.
"""
rectype_idvar: str
"""
The variable that identifies record types.
This is an empty string for rectangular extracts.
"""
rectype_keyvar: str
"""
The variable that uniquely identifies records across record types.
This is an empty string for rectangular extracts.
"""
encoding: str
"""IPUMS file encoding scheme"""
format: str
"""IPUMS extract data file format"""
place: str
"""IPUMS physical address"""
@classmethod
def read(cls, elt: Element, ddi_namespace: str) -> FileDescription:
"""
Read a FileDescription from the parsed XML
Args:
elt: xml element tree from parsed extract ddi
ddi_namespace: ddi namespace that says what the xmlns is for the file
Returns:
FileDescription object
"""
namespaces = {"ddi": ddi_namespace}
# only hierarchical files have recGrp information
try:
file_rectypes = elt.findall("./ddi:fileStrc/ddi:recGrp", namespaces)
rts = [rectype.attrib["rectype"] for rectype in file_rectypes]
except KeyError:
rts = []
# rectype get rectype id var and rectype key var
# these should be the same across record types for all collections
# so we should be fine to just grab the first appearance of recidvar and keyvar
try:
rectype_idvar = elt.find("./ddi:fileStrc/ddi:recGrp", namespaces).attrib[
"recidvar"
]
rectype_keyvar = elt.find("./ddi:fileStrc/ddi:recGrp", namespaces).attrib[
"keyvar"
]
except AttributeError:
rectype_idvar = ""
rectype_keyvar = ""
return cls(
filename=elt.find("./ddi:fileName", namespaces).text,
description=elt.find("./ddi:fileCont", namespaces).text,
structure=elt.find("./ddi:fileStrc", namespaces).attrib["type"],
rectypes=rts,
rectype_idvar=rectype_idvar,
rectype_keyvar=rectype_keyvar,
encoding=elt.find("./ddi:fileType", namespaces)
.attrib.get("charset", "iso-8859-1")
.lower(),
format=elt.find("./ddi:format", namespaces).text,
place=elt.find("./ddi:filePlac", namespaces).text,
)
@dataclass(frozen=True)
class Codebook:
"""
A class representing an XML codebook downloaded from IPUMS
"""
file_description: FileDescription
"""FileDescription object"""
data_description: List[VariableDescription]
"""list of VariableDescription objects"""
samples_description: List[str]
"""list of IPUMS sample descriptions"""
ipums_citation: str
"""The appropriate citation for the IPUMS extract. Please use it!"""
ipums_conditions: str
"""IPUMS terms of use"""
ipums_collection: str
"""IPUMS collection name"""
ipums_doi: str
""""DOI of IPUMS data set"""
@classmethod
def read(cls, elt: ET, ddi_namespace: str) -> Codebook:
"""
Read a Codebook from the parsed XML
Args:
elt: xml element tree from parsed extract ddi
ddi_namespace: ddi namespace that says what the xmlns is for the file
Returns:
Codebook object
"""
namespaces = {"ddi": ddi_namespace}
file_txts = elt.findall("./ddi:fileDscr/ddi:fileTxt", namespaces)
if len(file_txts) != 1:
raise NotImplementedError(
"Codebooks with more than one file type are not supported"
)
# compensation for lack of metadata api
_sample_descriptions = []
for item in elt.findall("./ddi:stdyDscr/ddi:stdyInfo/ddi:notes", namespaces):
sample_name_row = item.text.strip().split("\n")[0]
_sample_descriptions.append(sample_name_row)
ipums_samples = [desc.split(":")[-1].strip() for desc in _sample_descriptions]
ipums_citation = elt.find(
"./ddi:stdyDscr/ddi:dataAccs/ddi:useStmt/ddi:citReq", namespaces
).text
ipums_conditions = elt.find(
"./ddi:stdyDscr/ddi:dataAccs/ddi:useStmt/ddi:conditions", namespaces
).text
ipums_collection = elt.find(
"./ddi:stdyDscr/ddi:citation/ddi:serStmt/ddi:serName", namespaces
).attrib["abbr"]
ipums_doi = elt.find(
"./ddi:stdyDscr/ddi:citation/ddi:serStmt/ddi:serInfo", namespaces
).text
return cls(
file_description=FileDescription.read(file_txts[0], ddi_namespace),
data_description=[
VariableDescription.read(desc, ddi_namespace)
for desc in elt.findall("./ddi:dataDscr/ddi:var", namespaces)
],
samples_description=ipums_samples,
ipums_citation=ipums_citation,
ipums_conditions=ipums_conditions,
ipums_collection=ipums_collection,
ipums_doi=ipums_doi,
)
def get_variable_info(self, name: str) -> VariableDescription:
"""
Retrieve the VariableDescription for an IPUMS variable
Args:
name: Name of a variable in your IPUMS extract
Returns:
A VariableDescription instance
"""
try:
return [
vardesc
for vardesc in self.data_description
if vardesc.id == name.upper()
][0]
except IndexError:
raise ValueError(f"No description found for {name}.")
def get_all_types(self, type_format: str, string_pyarrow: bool = False) -> dict:
"""
Retrieve all column types
Args:
type_format: type format. Should be one of ["numpy_type", "pandas_type", "pandas_type_efficient",
"python_type", "vartype"]
string_pyarrow: has an effect when True and used with type_format in ["pandas_type", "pandas_type_efficient"].
In this case, string types==pd.StringDtype() is replaced with pd.StringDtype(storage='pyarrow').
Returns:
A dict with column names column dtype mapping.
Examples:
Let's see an example of usage with pandas.read_csv engine:
>>> from ipumspy import readers
>>> ddi_codebook = readers.read_ipums_ddi('extract_ddi.xml')
>>> dataframe_dtypes = ddi_codebook.get_all_types(type_format='pandas_type', string_pyarrow=False)
>>> df = readers.read_microdata(ddi=ddi_codebook, filename="extract.csv", dtype=dataframe_dtypes)
And an example of usecase of string_pyarrow set to True:
>>> from ipumspy import readers
>>> ddi_codebook = readers.read_ipums_ddi('extract_ddi.xml')
>>> dataframe_dtypes = ddi_codebook.get_all_types(type_format='pandas_type', string_pyarrow=True)
>>> # No particular impact for reading from csv.
>>> df = readers.read_microdata(ddi=ddi_codebook, filename="extract.csv", dtype=dataframe_dtypes)
>>> # The benefit of using string_pyarrow: converting to parquet. The writing time is reduced.
>>> df.to_parquet("extract.parquet")
>>> # Also, the data loaded from the derived extract.parquet will be faster than if the csv file was converted
>>> # using string_pyarrow=False
"""
if (
type_format not in ["pandas_type", "pandas_type_efficient"]
and string_pyarrow is True
):
raise ValueError(
'string_pyarrow can be set to True only if type_format in ["pandas_type", "pandas_type_efficient"].'
)
try:
# traversing the doc.
all_types = {}
for variable_descr in self.data_description:
type_value = getattr(variable_descr, type_format)
if type_value == pd.StringDtype() and string_pyarrow is True:
type_value = pd.StringDtype(storage="pyarrow")
all_types.update({variable_descr.name: type_value})
return all_types
except AttributeError:
acceptable_values = [
"numpy_type",
"pandas_type",
"pandas_type_efficient",
"python_type",
"vartype",
]
raise ValueError(f"{type_format} not in {acceptable_values}")