Convert a <suds.sax.text.Text> object to a string?

354 views Asked by At

I have a column in my dataframe that is of type suds.sax.text.Text and I want to convert it to a string. I can't find much on how to do this, except for this site. Using pandas.DataFrame.astype does not work. I'm sure there is an easy way to do this. The documentation is just going over my head. I am using a web service to use some of its functions to return some metadata on weather stations. This metadata gets returned back as a suds object. Link to the web service is here.

from suds.client import Client
from suds.transport.https import HttpAuthenticated
from urllib.error import URLError
from urllib.request import HTTPSHandler
import ssl
import pandas as pd

ssl._create_default_https_context = ssl._create_unverified_context
_URL_AWDB_WSDL = 'https://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL'

def _execute_awdb_call(a_func, ntries_max=3, sleep_sec=5, **kwargs):

    ntries = 0

    while 1:

        try:

            a_result = a_func(**kwargs)
            break

        except Exception as e:
            ntries += 1

            if ntries == ntries_max:

                raise

            else:

                print(("WARNING: Received error executing AWDB function %s:"
                       " %s. Sleeping %d seconds and trying again." % 
                       (str(a_func.method.name), str(e), sleep_sec)))

                sleep(sleep_sec)

    return a_result

def _stationMetadata_to_tuple(a_meta):

        list_meta = [None] * len(_stnmeta_attrs)

        for i, a_attr in enumerate(_stnmeta_attrs):

            try:
                list_meta[i] = a_meta[a_attr]
            except AttributeError:
                # Doesn't have attribute
                continue

        return tuple(list_meta)







try:
    _client = Client(_URL_AWDB_WSDL)
    _stnmeta_attrs = (_client.factory.
                          create('stationMetaData').__keylist__)
except URLError as e:
    if type(e.reason) == ssl.SSLError:
        print("Warning: SSL Error connecting to AWDB web service. Skipping verification...")
        _client = Client(_URL_AWDB_WSDL, transport=_CustomTransport())
        _stnmeta_attrs = (_client.factory.
                          create('stationMetaData').__keylist__)
    else:
        raise

stn_triplets = ['878:WY:SNTL', '1033:CO:SNTL']

stn_metas = _execute_awdb_call(_client.service.
                                     getStationMetadataMultiple,
                                     stationTriplets=stn_triplets)

stn_tups = [_stationMetadata_to_tuple(a) for a in stn_metas]

df_stns = pd.DataFrame(stn_tups, columns=_stnmeta_attrs)

stns = df_stns.rename(columns={'actonId': 'station_id',
                               'name': 'station_name'})

stns['station_id'] = stns.station_id.fillna(stns.stationTriplet)

stns = stns[~stns.station_id.isnull()]
print(type(stns.beginDate[0]))
0

There are 0 answers