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

GeoSeries to_sql not support #654

Open
czpmango opened this issue May 27, 2020 · 1 comment
Open

GeoSeries to_sql not support #654

czpmango opened this issue May 27, 2020 · 1 comment
Assignees
Labels
arctern-0.2.0 arctern-0.2.0 bug Something isn't working

Comments

@czpmango
Copy link
Collaborator

czpmango commented May 27, 2020

Describe the bug
GeoSeries did not behave as expected when call to_sql.

To Reproduce
My test code:

import pandas as pd
from arctern import GeoSeries
import arctern

def trans2wkb4series(s,index=range(0,0)):
    if isinstance(index,range):
        index = range(0,s.size)
    import pygeos
    s_arr = []
    if not isinstance(s, pd.Series):
        return None
    try:
        len = s.size
        for i in range(0, len):
            if not s[i]:
                s_arr.append(None)
            else:
                s_arr.append(pygeos.to_wkb(pygeos.Geometry(s[i])))
        s = pd.Series(s_arr,index=index)
    except:
        return None
    return s

geo_s = GeoSeries(["POINT (9 0)","POLYGON ((1 1,1 2,2 2,1 1))"])
pd_s = pd.Series(["POINT (9 0)","POLYGON ((1 1,1 2,2 2,1 1))"])
pd_s_wkb = trans2wkb4series(pd_s)
pd.testing.assert_series_equal(geo_s.astype(object),pd_s_wkb.astype(object),check_dtype=False) # (as expected)

geo_df=pd.DataFrame({'val':geo_s})
pd_df=pd.DataFrame({'val':pd_s_wkb})
pd.testing.assert_frame_equal(geo_df,pd_df,check_dtype=False)

from sqlalchemy import create_engine
engine = create_engine('sqlite://', echo=False)
# geo_res1 = geo_df.to_sql('users', con=engine, if_exists='replace', index_label='id')  # can not work
pd_res1 = pd_df.to_sql('users', con=engine, if_exists='replace', index_label='id')
engine.execute("SELECT * FROM users").fetchall() # as expected

Expected behavior
Expect the same results as the panda series when call to_sql.(pandas API :
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.htmll)

@czpmango czpmango added the bug Something isn't working label May 27, 2020
@czpmango czpmango added the arctern-0.2.0 arctern-0.2.0 label May 27, 2020
@shengjh
Copy link
Collaborator

shengjh commented May 28, 2020

Reproduce
Pandas Version: v1.0.3
Here is a simplified code to reproduce it.

import pandas as pd
from arctern import GeoSeries
geo_s = GeoSeries(["POINT (9 0)","POLYGON ((1 1,1 2,2 2,1 1))"])
geo_df=pd.DataFrame({'val':geo_s})
from sqlalchemy import create_engine
engine = create_engine('sqlite://', echo=False)
geo_res1 = geo_df.to_sql('users', con=engine, if_exists='replace', index_label='id')

And it will crash at
File "pandas/_libs/lib.pyx", line 1127, in pandas._libs.lib._try_infer_map AttributeError: 'GeoDtype' object has no attribute 'base'

I check out pandas source code.
https://github.com/pandas-dev/pandas/blob/3adf3340453d6704d4a2cb47058214cc697a7d29/pandas/_libs/lib.pyx#L1120-L1130
The reason is pandas tries to infer data type for our GeoDtype, by getattr('name' or 'kind' or 'base'). But class GeoDtype does not have attribute 'base', then it crashed with AttributeError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arctern-0.2.0 arctern-0.2.0 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants