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

Python 3 timestamp_tz_parse TypeError #117

Closed
trianta2 opened this issue May 11, 2016 · 1 comment
Closed

Python 3 timestamp_tz_parse TypeError #117

trianta2 opened this issue May 11, 2016 · 1 comment

Comments

@trianta2
Copy link

The following function:

def timestamp_tz_parse(s):
    # if timezome is simply UTC...
    if s.endswith('+00'):
        # remove time zone
        ts = timestamp_parse(s[:-3])
        ts = ts.replace(tzinfo=pytz.UTC)
        return ts
    # other wise do a real parse (slower)
    return parser.parse(s)

raises TypeError: endswith first arg must be bytes or a tuple of bytes, not str at this line.

It appears that s is a bytes object, not str. Changing if s.endswith('+00'): to if s.endswith(b'+00'): resolves the issue for me.

I'm using:

  • Python 3.4.4
  • vertica-python==0.6.2
@trianta2 trianta2 changed the title Python 3 fetch issue Python 3 timestamp_tz_parse TypeError May 11, 2016
@dennisobrien
Copy link
Contributor

A Vertica TimestampTz column gets parsed by timestamp_tz_parse while a Timestamp column gets parsed by timestamp_parse. The timestamp_parse method ends up calling datetime.strptime which does require a string. In this case it might be better to just coerce to strings in these functions.

This code path calls timestamp_parse:

cursor = connection.cursor()
query = """
select
  to_timestamp('2016-05-15 13:15:17.789', 'YYYY-MM-DD HH:MI:SS.MS')
;
"""
cursor.execute(query)
cursor.fetchall()

and throws this exception:

/usr/local/lib/python3.5/dist-packages/vertica_python-0.6.2-py3.5.egg/vertica_python/vertica/column.py in _timestamp_parse(s)
     53     if len(s) == 19:
     54         return datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
---> 55     return datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')
     56 
     57 

TypeError: strptime() argument 1 must be str, not bytes

While this code path calls timestamp_tz_parse:

query = """
select
  to_timestamp_tz('2016-05-15 13:15:17.789 UTC', 'YYYY-MM-DD HH:MI:SS.MS TZ')
;
"""
cursor.execute(query)
cursor.fetchall()

throws this exception

/usr/local/lib/python3.5/dist-packages/vertica_python-0.6.2-py3.5.egg/vertica_python/vertica/column.py in timestamp_tz_parse(s)
     64 def timestamp_tz_parse(s):
     65     # if timezome is simply UTC...
---> 66     if s.endswith('+00'):
     67         # remove time zone
     68         ts = timestamp_parse(s[:-3])

TypeError: endswith first arg must be bytes or a tuple of bytes, not str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants