Skip to content

Commit 9f56093

Browse files
authored
Merge pull request #664 from addisonlynch/0.7
COMPAT: Repair PY2 issues
2 parents 7b598ce + 2b4a805 commit 9f56093

File tree

16 files changed

+75
-50
lines changed

16 files changed

+75
-50
lines changed

docs/source/whatsnew/v0.7.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Backwards incompatible API changes
6464
Bug Fixes
6565
~~~~~~~~~
6666

67+
- Fixed import of pandas.compat (:issue:`657`)
6768
- Added support for passing the API KEY to QuandlReader either directly or by
6869
setting the environmental variable QUANDL_API_KEY (:issue:`485`).
6970
- Added support for optionally passing a custom base_url to the EnigmaReader (:issue:`499`).

pandas_datareader/bankofcanada.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import unicode_literals
22

3-
import pandas.compat as compat
3+
from pandas_datareader.compat import string_types
44

55
from pandas_datareader.base import _BaseReader
66

@@ -17,7 +17,7 @@ class BankOfCanadaReader(_BaseReader):
1717
@property
1818
def url(self):
1919
"""API URL"""
20-
if not isinstance(self.symbols, compat.string_types):
20+
if not isinstance(self.symbols, string_types):
2121
raise ValueError('data name must be string')
2222

2323
return '{0}/{1}/csv'.format(self._URL, self.symbols)

pandas_datareader/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
import requests
66

7-
import pandas.compat as compat
8-
from io import StringIO
97
from pandas import DataFrame
108
from pandas import read_csv, concat
119
from pandas.io.common import urlencode
12-
from pandas.compat import bytes_to_str
10+
from pandas_datareader.compat import bytes_to_str, string_types, binary_type, \
11+
StringIO
1312

1413
from pandas_datareader._utils import (RemoteDataError, SymbolWarning,
1514
_sanitize_dates, _init_session)
@@ -100,7 +99,7 @@ def _read_url_as_StringIO(self, url, params=None):
10099
service = self.__class__.__name__
101100
raise IOError("{} request returned no data; check URL for invalid "
102101
"inputs: {}".format(service, self.url))
103-
if isinstance(text, compat.binary_type):
102+
if isinstance(text, binary_type):
104103
out.write(bytes_to_str(text))
105104
else:
106105
out.write(text)
@@ -206,7 +205,7 @@ def _get_params(self, *args, **kwargs):
206205
def read(self):
207206
"""Read data"""
208207
# If a single symbol, (e.g., 'GOOG')
209-
if isinstance(self.symbols, (compat.string_types, int)):
208+
if isinstance(self.symbols, (string_types, int)):
210209
df = self._read_one_data(self.url,
211210
params=self._get_params(self.symbols))
212211
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])

pandas_datareader/compat/__init__.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# flake8: noqa
2-
import sys
3-
from distutils.version import LooseVersion
4-
from io import BytesIO
5-
62
import pandas as pd
7-
import pandas.compat as compat
83
import pandas.io.common as com
4+
import sys
5+
from distutils.version import LooseVersion
96

107
PY3 = sys.version_info >= (3, 0)
118

@@ -45,11 +42,42 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
4542
else:
4643
from pandas.core.common import is_list_like
4744

48-
49-
if compat.PY3:
45+
if PY3:
5046
from urllib.error import HTTPError
47+
from functools import reduce
48+
49+
string_types = str,
50+
binary_type = bytes
51+
from io import StringIO
52+
53+
def str_to_bytes(s, encoding=None):
54+
return s.encode(encoding or 'ascii')
55+
56+
57+
def bytes_to_str(b, encoding=None):
58+
return b.decode(encoding or 'utf-8')
5159
else:
5260
from urllib2 import HTTPError
61+
from cStringIO import StringIO
62+
reduce = reduce
63+
binary_type = str
64+
string_types = basestring,
65+
66+
67+
def bytes_to_str(b, encoding=None):
68+
return b
69+
70+
71+
def str_to_bytes(s, encoding=None):
72+
return s
73+
74+
75+
def lmap(*args, **kwargs):
76+
return list(map(*args, **kwargs))
77+
78+
79+
def lrange(*args, **kwargs):
80+
return list(range(*args, **kwargs))
5381

5482

5583
def concat(*args, **kwargs):

pandas_datareader/enigma.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import os
22
import time
33

4-
from io import StringIO
5-
import pandas.compat as compat
64
import pandas as pd
75

8-
from pandas_datareader.base import _BaseReader
6+
from pandas_datareader.base import _BaseReader, string_types
7+
from pandas_datareader.compat import StringIO
98

109

1110
class EnigmaReader(_BaseReader):
@@ -65,7 +64,7 @@ def __init__(self,
6564
self._api_key = api_key
6665

6766
self._dataset_id = dataset_id
68-
if not isinstance(self._dataset_id, compat.string_types):
67+
if not isinstance(self._dataset_id, string_types):
6968
raise ValueError(
7069
"The Enigma dataset_id must be a string (ex: "
7170
"'bedaf052-5fcd-4758-8d27-048ce8746c6a')")

pandas_datareader/eurostat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import unicode_literals
22

33
import pandas as pd
4-
import pandas.compat as compat
54

65
from pandas_datareader.io.sdmx import read_sdmx, _read_sdmx_dsd
6+
from pandas_datareader.compat import string_types
77
from pandas_datareader.base import _BaseReader
88

99

@@ -15,7 +15,7 @@ class EurostatReader(_BaseReader):
1515
@property
1616
def url(self):
1717
"""API URL"""
18-
if not isinstance(self.symbols, compat.string_types):
18+
if not isinstance(self.symbols, string_types):
1919
raise ValueError('data name must be string')
2020

2121
q = '{0}/data/{1}/?startperiod={2}&endperiod={3}'
@@ -25,7 +25,7 @@ def url(self):
2525
@property
2626
def dsd_url(self):
2727
"""API DSD URL"""
28-
if not isinstance(self.symbols, compat.string_types):
28+
if not isinstance(self.symbols, string_types):
2929
raise ValueError('data name must be string')
3030

3131
return '{0}/datastructure/ESTAT/DSD_{1}'.format(

pandas_datareader/famafrench.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import datetime as dt
22
import re
33
import tempfile
4-
from io import StringIO
4+
55
from zipfile import ZipFile
66

77
from pandas import read_csv, to_datetime
8-
from pandas.compat import lmap
8+
from pandas_datareader.compat import lmap, StringIO
99

1010
from pandas_datareader.base import _BaseReader
1111

pandas_datareader/io/jsdmx.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# pylint: disable-msg=E1101,W0613,W0603
22

33
from __future__ import unicode_literals
4+
from collections import OrderedDict
45

56
import itertools
67
import sys
78

89
import numpy as np
910
import pandas as pd
10-
import pandas.compat as compat
1111

1212
from pandas_datareader.io.util import _read_content
1313

@@ -38,7 +38,7 @@ def read_jsdmx(path_or_buf):
3838
if isinstance(jdata, dict):
3939
data = jdata
4040
else:
41-
data = json.loads(jdata, object_pairs_hook=compat.OrderedDict)
41+
data = json.loads(jdata, object_pairs_hook=OrderedDict)
4242

4343
structure = data['structure']
4444
index = _parse_dimensions(structure['dimensions']['observation'])
@@ -56,9 +56,9 @@ def read_jsdmx(path_or_buf):
5656

5757
def _get_indexer(index):
5858
if index.nlevels == 1:
59-
return [str(i) for i in compat.range(len(index))]
59+
return [str(i) for i in range(len(index))]
6060
else:
61-
it = itertools.product(*[compat.range(
61+
it = itertools.product(*[range(
6262
len(level)) for level in index.levels])
6363
return [':'.join(map(str, i)) for i in it]
6464

@@ -68,7 +68,7 @@ def _parse_values(dataset, index, columns):
6868
series = dataset['series']
6969

7070
values = []
71-
# for s_key, s_value in compat.iteritems(series):
71+
# for s_key, s_value in iteritems(series):
7272
for s_key in _get_indexer(columns):
7373
try:
7474
observations = series[s_key]['observations']

pandas_datareader/io/sdmx.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import collections
44
import time
55
import zipfile
6+
from io import BytesIO
67

78
import pandas as pd
8-
import pandas.compat as compat
99

1010
from pandas_datareader.io.util import _read_content
11-
from pandas_datareader.compat import HTTPError
11+
from pandas_datareader.compat import HTTPError, str_to_bytes
1212

1313

1414
_STRUCTURE = '{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure}'
@@ -235,8 +235,8 @@ def _read_zipped_sdmx(path_or_buf):
235235
""" Unzipp data contains SDMX-XML """
236236
data = _read_content(path_or_buf)
237237

238-
zp = compat.BytesIO()
239-
zp.write(compat.str_to_bytes(data))
238+
zp = BytesIO()
239+
zp.write(str_to_bytes(data))
240240
f = zipfile.ZipFile(zp)
241241
files = f.namelist()
242242
assert len(files) == 1

pandas_datareader/io/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import os
44

5-
import pandas.compat as compat
6-
from pandas_datareader.compat import get_filepath_or_buffer
5+
from pandas_datareader.compat import get_filepath_or_buffer, string_types
76

87

98
def _read_content(path_or_buf):
@@ -13,7 +12,7 @@ def _read_content(path_or_buf):
1312

1413
filepath_or_buffer = get_filepath_or_buffer(path_or_buf)[0]
1514

16-
if isinstance(filepath_or_buffer, compat.string_types):
15+
if isinstance(filepath_or_buffer, string_types):
1716
try:
1817
exists = os.path.exists(filepath_or_buffer)
1918
except (TypeError, ValueError):

pandas_datareader/moex.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import pandas as pd
66

7-
from io import StringIO
87
from pandas_datareader.base import _DailyBaseReader
9-
from pandas_datareader.compat import is_list_like, concat
8+
from pandas_datareader.compat import (binary_type, concat, is_list_like,
9+
StringIO)
10+
1011

1112

1213
class MoexReader(_DailyBaseReader):
@@ -103,7 +104,7 @@ def _get_metadata(self):
103104
service = self.__class__.__name__
104105
raise IOError("{} request returned no data; check URL for invalid "
105106
"inputs: {}".format(service, self.__url_metadata))
106-
if isinstance(text, pd.compat.binary_type):
107+
if isinstance(text, binary_type):
107108
text = text.decode('windows-1251')
108109

109110
header_str = 'secid;boardid;'
@@ -183,7 +184,7 @@ def _read_url_as_String(self, url, params=None):
183184
service = self.__class__.__name__
184185
raise IOError("{} request returned no data; check URL for invalid "
185186
"inputs: {}".format(service, self.url))
186-
if isinstance(text, pd.compat.binary_type):
187+
if isinstance(text, binary_type):
187188
text = text.decode('windows-1251')
188189
return text
189190

pandas_datareader/nasdaq_trader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from ftplib import FTP, all_errors
2-
from io import StringIO
2+
33
from pandas import read_csv
44
from pandas_datareader._utils import RemoteDataError
5+
from pandas_datareader.compat import StringIO
56

67
import time
78
import warnings

pandas_datareader/oecd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pandas as pd
2-
import pandas.compat as compat
32

43
from pandas_datareader.io import read_jsdmx
54
from pandas_datareader.base import _BaseReader
5+
from pandas_datareader.compat import string_types
66

77

88
class OECDReader(_BaseReader):
@@ -15,7 +15,7 @@ def url(self):
1515
"""API URL"""
1616
url = 'http://stats.oecd.org/SDMX-JSON/data'
1717

18-
if not isinstance(self.symbols, compat.string_types):
18+
if not isinstance(self.symbols, string_types):
1919
raise ValueError('data name must be string')
2020

2121
# API: https://data.oecd.org/api/sdmx-json-documentation/

pandas_datareader/wb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import warnings
44

5-
from pandas.compat import reduce, lrange, string_types
5+
from pandas_datareader.compat import reduce, lrange, string_types
66
import pandas as pd
77
import numpy as np
88

pandas_datareader/yahoo/fx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import warnings
44
from pandas import (DataFrame, Series, to_datetime, concat)
55
from pandas_datareader.yahoo.daily import YahooDailyReader
6-
import pandas.compat as compat
76
from pandas_datareader._utils import (RemoteDataError, SymbolWarning)
7+
from pandas_datareader.compat import string_types
88

99

1010
class YahooFXReader(YahooDailyReader):
@@ -55,7 +55,7 @@ def read(self):
5555
"""Read data"""
5656
try:
5757
# If a single symbol, (e.g., 'GOOG')
58-
if isinstance(self.symbols, (compat.string_types, int)):
58+
if isinstance(self.symbols, (string_types, int)):
5959
df = self._read_one_data(self.symbols)
6060

6161
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])

pandas_datareader/yahoo/quotes.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import json
22
from collections import OrderedDict
3-
4-
import pandas.compat as compat
53
from pandas import DataFrame
64

7-
85
from pandas_datareader.base import _BaseReader
9-
6+
from pandas_datareader.compat import string_types
107

118
_DEFAULT_PARAMS = {
129
'lang': 'en-US',
@@ -24,7 +21,7 @@ def url(self):
2421
return 'https://query1.finance.yahoo.com/v7/finance/quote'
2522

2623
def read(self):
27-
if isinstance(self.symbols, compat.string_types):
24+
if isinstance(self.symbols, string_types):
2825
return self._read_one_data(self.url, self.params(self.symbols))
2926
else:
3027
data = OrderedDict()

0 commit comments

Comments
 (0)