Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pysus/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 5 additions & 5 deletions pysus/utilities/readdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"""
import os
from tempfile import NamedTemporaryFile
from io import BytesIO
import pandas as pd
import geopandas as gpd
from dbfread import DBF

try:
Expand All @@ -16,21 +14,23 @@
from _readdbc import ffi, lib


def read_dbc(filename, encoding='utf-8', raw=False):
def read_dbc(filename, encoding='utf-8', raw=False, tmp_dir='.', delete_tmp=True):
"""
Opens a DATASUS .dbc file and return its contents as a pandas
Dataframe.
:param filename: .dbc filename
:param encoding: encoding of the data
:param raw: Skip type conversion. Set it to True to avoid type conversion errors
:param tmp_dir: Temp directory where tmp files are going to - useful if using ramdisks to reduce IO.
:param delete_tmp: Delete temporary files after function execution
:return: Pandas Dataframe.
"""
if isinstance(filename, str):
filename = filename.encode()
with NamedTemporaryFile(delete=False) as tf:
with NamedTemporaryFile(dir=tmp_dir, delete=delete_tmp) as tf:
dbc2dbf(filename, tf.name.encode())
dbf = DBF(tf.name, encoding=encoding, raw=raw)
df = gpd.GeoDataFrame(list(dbf))
df = pd.DataFrame(list(dbf))
os.unlink(tf.name)

return df
Expand Down