Getting the currency at midnight raises ValueError. #15

Closed
hayd opened this Issue Apr 20, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@hayd
Contributor

hayd commented Apr 20, 2015

It looks like (yahoo?) is returning a bad dateformat e.g. '4/21/2015 0:13am' Note: the 0 rather than 12 to indicate midnight. This means it isn't parseable with %I (which is used here).

In [1]: from yahoo_finance import Currency

In [2]: eur_pln = Currency("EURPLN")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-df89e7129008> in <module>()
----> 1 eur_pln = Currency("EURPLN")

/Users/andy/proj/yahoo-finance/yahoo_finance/__init__.py in __init__(self, symbol)
    147         self._table = 'xchange'
    148         self._key = 'pair'
--> 149         self.refresh()
    150
    151     def _fetch(self):

/Users/andy/proj/yahoo-finance/yahoo_finance/__init__.py in refresh(self)
    138
    139         """
--> 140         self.data_set = self._fetch()
    141
    142

/Users/andy/proj/yahoo-finance/yahoo_finance/__init__.py in _fetch(self)
    152         data = super(Currency, self)._fetch()
    153         if data['Date'] and data['Time']:
--> 154             data[u'DateTimeUTC'] = edt_to_utc('{0} {1}'.format(data['Date'], data['Time']))
    155         return data
    156

/Users/andy/proj/yahoo-finance/yahoo_finance/__init__.py in edt_to_utc(date, mask)
     19     utc = pytz.utc
     20     eastern = pytz.timezone('US/Eastern')
---> 21     date_ = datetime.strptime(date, mask)
     22     date_eastern = eastern.localize(date_, is_dst=None)
     23     date_utc = date_eastern.astimezone(utc)

/Users/andy/anaconda/envs/py34/lib/python3.4/_strptime.py in _strptime_datetime(cls, data_string, format)
    498     """Return a class cls instance based on the input string and the
    499     format string."""
--> 500     tt, fraction = _strptime(data_string, format)
    501     tzname, gmtoff = tt[-2:]
    502     args = tt[:6] + (fraction,)

/Users/andy/anaconda/envs/py34/lib/python3.4/_strptime.py in _strptime(data_string, format)
    335     if not found:
    336         raise ValueError("time data %r does not match format %r" %
--> 337                          (data_string, format))
    338     if len(data_string) != found.end():
    339         raise ValueError("unconverted data remains: %s" %

ValueError: time data '4/21/2015 0:13am' does not match format '%m/%d/%Y %I:%M%p'
@chriggi

This comment has been minimized.

Show comment
Hide comment
@chriggi

chriggi May 5, 2015

I'm experiencing exactly the same problem:

time data '5/6/2015 0:35am' does not match format '%m/%d/%Y %I:%M%p'

chriggi commented May 5, 2015

I'm experiencing exactly the same problem:

time data '5/6/2015 0:35am' does not match format '%m/%d/%Y %I:%M%p'

@hayd

This comment has been minimized.

Show comment
Hide comment
@hayd

hayd May 5, 2015

Contributor

One solution is just to replace the 0:.. with 12:...

In [11]: t, f = '4/21/2015 0:13am', '%m/%d/%Y %I:%M%p'

In [12]: datetime.datetime.strptime(t.replace(" 0:", " 12:"), f)
Out[12]: datetime.datetime(2015, 4, 21, 0, 13)

bit of a hack, but not sure there is a % for this.

Contributor

hayd commented May 5, 2015

One solution is just to replace the 0:.. with 12:...

In [11]: t, f = '4/21/2015 0:13am', '%m/%d/%Y %I:%M%p'

In [12]: datetime.datetime.strptime(t.replace(" 0:", " 12:"), f)
Out[12]: datetime.datetime(2015, 4, 21, 0, 13)

bit of a hack, but not sure there is a % for this.

hayd added a commit to hayd/yahoo-finance that referenced this issue May 5, 2015

Accept the returned date at midnight
Fix #15.

yahoo returns a date of the form '4/21/2015 0:13am', rather than '4/21/2015 12:13am'.

There is not format to parse this so just replace it.

@lukaszbanasiak lukaszbanasiak closed this in #17 May 6, 2015

lukaszbanasiak added a commit that referenced this issue May 6, 2015

@lukaszbanasiak

This comment has been minimized.

Show comment
Hide comment
@lukaszbanasiak

lukaszbanasiak May 6, 2015

Owner

Merged and pubished in version 1.2.1
I have added test case for this.

thx! :)

Owner

lukaszbanasiak commented May 6, 2015

Merged and pubished in version 1.2.1
I have added test case for this.

thx! :)

@chriggi

This comment has been minimized.

Show comment
Hide comment
@chriggi

chriggi May 6, 2015

Thanks a lot!!

chriggi commented May 6, 2015

Thanks a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment