Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read null geoms from carto #1637

Merged
merged 3 commits into from
May 19, 2020
Merged
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
36 changes: 11 additions & 25 deletions cartoframes/utils/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .utils import dtypes2pg, pg2dtypes, PG_NULL

BOOL_DBTYPES = ['bool', 'boolean']
OBJECT_DBTYPES = ['text']
INT_DBTYPES = ['int2', 'int4', 'int2', 'int', 'int8', 'smallint', 'integer', 'bigint']
FLOAT_DBTYPES = ['float4', 'float8', 'real', 'double precision', 'numeric', 'decimal']
DATETIME_DBTYPES = ['date', 'timestamp', 'timestampz']
Expand Down Expand Up @@ -185,27 +184,21 @@ def _is_unsupported(value):
def obtain_converters(columns):
converters = {}

for int_column_name in type_columns_names(columns, INT_DBTYPES):
converters[int_column_name] = _convert_int

for float_column_name in type_columns_names(columns, FLOAT_DBTYPES):
converters[float_column_name] = _convert_float

for bool_column_name in type_columns_names(columns, BOOL_DBTYPES):
converters[bool_column_name] = _convert_bool

for object_column_name in type_columns_names(columns, OBJECT_DBTYPES):
converters[object_column_name] = _convert_object
for column in columns:
if column.dbtype in INT_DBTYPES:
converters[column.name] = _convert_int
elif column.dbtype in FLOAT_DBTYPES:
converters[column.name] = _convert_float
elif column.dbtype in BOOL_DBTYPES:
converters[column.name] = _convert_bool
else:
converters[column.name] = _convert_generic

return converters


def date_columns_names(columns):
return type_columns_names(columns, DATETIME_DBTYPES)


def type_columns_names(columns, dbtypes):
return [x.name for x in columns if x.dbtype in dbtypes]
return [x.name for x in columns if x.dbtype in DATETIME_DBTYPES]


def _convert_int(x):
Expand All @@ -230,18 +223,11 @@ def _convert_bool(x):
return bool(x)


def _convert_object(x):
def _convert_generic(x):
if _is_none_null(x):
return None
return x


def _is_none_null(x):
return x is None or x == PG_NULL


def _first_value(series):
series = series.loc[~series.isnull()] # Remove null values
if len(series) > 0:
return series.iloc[0]
return None
1 change: 0 additions & 1 deletion cartoframes/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ def get_geodataframe_data(data, encode_data=True):
return data


# Dup
def _first_value(series):
series = series.loc[~series.isnull()] # Remove null values
if len(series) > 0:
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/utils/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from geopandas import GeoDataFrame

from cartoframes.utils.geom_utils import set_geometry
from cartoframes.utils.columns import ColumnInfo, get_dataframe_columns_info, normalize_names
from cartoframes.utils.columns import ColumnInfo, get_dataframe_columns_info, normalize_names, \
obtain_converters, _convert_int, _convert_float, \
_convert_bool, _convert_generic


class TestColumns(object):
Expand Down Expand Up @@ -113,3 +115,21 @@ def test_column_info_geometry_troubled_names(self):
ColumnInfo('the_geom', 'the_geom', 'geometry(Geometry, 4326)', True),
ColumnInfo('g-e-o-m-e-t-r-y', 'g_e_o_m_e_t_r_y', 'text', False)
]

def test_converters(self):
columns = [
ColumnInfo('cartodb_id', 'cartodb_id', 'integer', False),
ColumnInfo('the_geom', 'the_geom', 'geometry(Geometry, 4326)', True),
ColumnInfo('name', 'name', 'text', False),
ColumnInfo('flag', 'flag', 'boolean', False),
ColumnInfo('number', 'number', 'double precision', False)
]

converters = obtain_converters(columns)

assert type(converters) == dict
assert converters['cartodb_id'] == _convert_int
assert converters['the_geom'] == _convert_generic
assert converters['name'] == _convert_generic
assert converters['flag'] == _convert_bool
assert converters['number'] == _convert_float