Skip to content

Commit

Permalink
Merge pull request #657 from bashtage/702
Browse files Browse the repository at this point in the history
BUG: Remove pandas.compat dependencies
  • Loading branch information
bashtage authored Jul 28, 2019
2 parents 68da604 + 4361bbf commit 35189ec
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 46 deletions.
1 change: 1 addition & 0 deletions docs/source/whatsnew/v0.7.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Backwards incompatible API changes
Bug Fixes
~~~~~~~~~

- Fixed import of pandas.compat (:issue:`657`)
- Added support for passing the API KEY to QuandlReader either directly or by
setting the environmental variable QUANDL_API_KEY (:issue:`485`).
- Added support for optionally passing a custom base_url to the EnigmaReader (:issue:`499`).
Expand Down
4 changes: 2 additions & 2 deletions pandas_datareader/bankofcanada.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

import pandas.compat as compat
from pandas_datareader.compat import string_types

from pandas_datareader.base import _BaseReader

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

return '{0}/{1}/csv'.format(self._URL, self.symbols)
Expand Down
8 changes: 4 additions & 4 deletions pandas_datareader/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import numpy as np

import requests
from io import StringIO

import pandas.compat as compat
from pandas import DataFrame
from pandas import read_csv, concat
from pandas.io.common import urlencode
from pandas.compat import StringIO, bytes_to_str
from pandas_datareader.compat import bytes_to_str, string_types, binary_type

from pandas_datareader._utils import (RemoteDataError, SymbolWarning,
_sanitize_dates, _init_session)
Expand Down Expand Up @@ -99,7 +99,7 @@ def _read_url_as_StringIO(self, url, params=None):
service = self.__class__.__name__
raise IOError("{} request returned no data; check URL for invalid "
"inputs: {}".format(service, self.url))
if isinstance(text, compat.binary_type):
if isinstance(text, binary_type):
out.write(bytes_to_str(text))
else:
out.write(text)
Expand Down Expand Up @@ -205,7 +205,7 @@ def _get_params(self, *args, **kwargs):
def read(self):
"""Read data"""
# If a single symbol, (e.g., 'GOOG')
if isinstance(self.symbols, (compat.string_types, int)):
if isinstance(self.symbols, (string_types, int)):
df = self._read_one_data(self.url,
params=self._get_params(self.symbols))
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
Expand Down
41 changes: 35 additions & 6 deletions pandas_datareader/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# flake8: noqa
import pandas as pd
import pandas.io.common as com
import sys
from distutils.version import LooseVersion
from io import BytesIO

import pandas as pd
import pandas.compat as compat
import pandas.io.common as com

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

PANDAS_VERSION = LooseVersion(pd.__version__)
Expand Down Expand Up @@ -45,8 +43,39 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
else:
from pandas.core.common import is_list_like


if compat.PY3:
if PY3:
from urllib.error import HTTPError
from functools import reduce

string_types = str,
binary_type = bytes


def str_to_bytes(s, encoding=None):
return s.encode(encoding or 'ascii')


def bytes_to_str(b, encoding=None):
return b.decode(encoding or 'utf-8')
else:
from urllib2 import HTTPError

reduce = reduce
binary_type = str
string_types = basestring,


def bytes_to_str(b, encoding=None):
return b


def str_to_bytes(s, encoding=None):
return s


def lmap(*args, **kwargs):
return list(map(*args, **kwargs))


def lrange(*args, **kwargs):
return list(range(*args, **kwargs))
5 changes: 2 additions & 3 deletions pandas_datareader/enigma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import time

from io import StringIO
import pandas.compat as compat
import pandas as pd

from pandas_datareader.base import _BaseReader
from pandas_datareader.base import _BaseReader, string_types


class EnigmaReader(_BaseReader):
Expand Down Expand Up @@ -65,7 +64,7 @@ def __init__(self,
self._api_key = api_key

self._dataset_id = dataset_id
if not isinstance(self._dataset_id, compat.string_types):
if not isinstance(self._dataset_id, string_types):
raise ValueError(
"The Enigma dataset_id must be a string (ex: "
"'bedaf052-5fcd-4758-8d27-048ce8746c6a')")
Expand Down
6 changes: 3 additions & 3 deletions pandas_datareader/eurostat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import unicode_literals

import pandas as pd
import pandas.compat as compat

from pandas_datareader.io.sdmx import read_sdmx, _read_sdmx_dsd
from pandas_datareader.compat import string_types
from pandas_datareader.base import _BaseReader


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

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

return '{0}/datastructure/ESTAT/DSD_{1}'.format(
Expand Down
4 changes: 2 additions & 2 deletions pandas_datareader/famafrench.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from zipfile import ZipFile

from pandas import read_csv, to_datetime
from pandas.compat import lmap
from pandas_datareader.compat import lmap

from pandas_datareader.base import _BaseReader

Expand Down Expand Up @@ -107,7 +107,7 @@ def _read_one_data(self, url, params):

datasets, table_desc = {}, []
for i, src in enumerate(tables):
match = re.search('^\s*,', src, re.M) # the table starts there
match = re.search(r'^\s*,', src, re.M) # the table starts there
start = 0 if not match else match.start()

df = read_csv(StringIO('Date' + src[start:]), **params)
Expand Down
9 changes: 5 additions & 4 deletions pandas_datareader/google/quotes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
import numpy as np
import pandas as pd
import re
from dateutil.parser import parse
import numpy as np

from pandas_datareader.base import _BaseReader
import json
import re
from pandas_datareader.compat import string_types


class GoogleQuotesReader(_BaseReader):
Expand All @@ -20,7 +21,7 @@ def url(self):
@property
def params(self):
"""Parameters to use in API calls"""
if isinstance(self.symbols, pd.compat.string_types):
if isinstance(self.symbols, string_types):
sym_list = self.symbols
else:
sym_list = ','.join(self.symbols)
Expand Down
10 changes: 5 additions & 5 deletions pandas_datareader/io/jsdmx.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# pylint: disable-msg=E1101,W0613,W0603

from __future__ import unicode_literals
from collections import OrderedDict

import itertools
import sys

import numpy as np
import pandas as pd
import pandas.compat as compat

from pandas_datareader.io.util import _read_content

Expand Down Expand Up @@ -38,7 +38,7 @@ def read_jsdmx(path_or_buf):
if isinstance(jdata, dict):
data = jdata
else:
data = json.loads(jdata, object_pairs_hook=compat.OrderedDict)
data = json.loads(jdata, object_pairs_hook=OrderedDict)

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

def _get_indexer(index):
if index.nlevels == 1:
return [str(i) for i in compat.range(len(index))]
return [str(i) for i in range(len(index))]
else:
it = itertools.product(*[compat.range(
it = itertools.product(*[range(
len(level)) for level in index.levels])
return [':'.join(map(str, i)) for i in it]

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

values = []
# for s_key, s_value in compat.iteritems(series):
# for s_key, s_value in iteritems(series):
for s_key in _get_indexer(columns):
try:
observations = series[s_key]['observations']
Expand Down
8 changes: 4 additions & 4 deletions pandas_datareader/io/sdmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import collections
import time
import zipfile
from io import BytesIO

import pandas as pd
import pandas.compat as compat

from pandas_datareader.io.util import _read_content
from pandas_datareader.compat import HTTPError
from pandas_datareader.compat import HTTPError, str_to_bytes


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

zp = compat.BytesIO()
zp.write(compat.str_to_bytes(data))
zp = BytesIO()
zp.write(str_to_bytes(data))
f = zipfile.ZipFile(zp)
files = f.namelist()
assert len(files) == 1
Expand Down
5 changes: 2 additions & 3 deletions pandas_datareader/io/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import os

import pandas.compat as compat
from pandas_datareader.compat import get_filepath_or_buffer
from pandas_datareader.compat import get_filepath_or_buffer, string_types


def _read_content(path_or_buf):
Expand All @@ -13,7 +12,7 @@ def _read_content(path_or_buf):

filepath_or_buffer = get_filepath_or_buffer(path_or_buf)[0]

if isinstance(filepath_or_buffer, compat.string_types):
if isinstance(filepath_or_buffer, string_types):
try:
exists = os.path.exists(filepath_or_buffer)
except (TypeError, ValueError):
Expand Down
4 changes: 2 additions & 2 deletions pandas_datareader/oecd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pandas as pd
import pandas.compat as compat

from pandas_datareader.io import read_jsdmx
from pandas_datareader.base import _BaseReader
from pandas_datareader.compat import string_types


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

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

# API: https://data.oecd.org/api/sdmx-json-documentation/
Expand Down
2 changes: 1 addition & 1 deletion pandas_datareader/wb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings

from pandas.compat import reduce, lrange, string_types
from pandas_datareader.compat import reduce, lrange, string_types
import pandas as pd
import numpy as np

Expand Down
4 changes: 2 additions & 2 deletions pandas_datareader/yahoo/fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import warnings
from pandas import (DataFrame, Series, to_datetime, concat)
from pandas_datareader.yahoo.daily import YahooDailyReader
import pandas.compat as compat
from pandas_datareader._utils import (RemoteDataError, SymbolWarning)
from pandas_datareader.compat import string_types


class YahooFXReader(YahooDailyReader):
Expand Down Expand Up @@ -55,7 +55,7 @@ def read(self):
"""Read data"""
try:
# If a single symbol, (e.g., 'GOOG')
if isinstance(self.symbols, (compat.string_types, int)):
if isinstance(self.symbols, (string_types, int)):
df = self._read_one_data(self.symbols)

# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
Expand Down
7 changes: 2 additions & 5 deletions pandas_datareader/yahoo/quotes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import json
from collections import OrderedDict

import pandas.compat as compat
from pandas import DataFrame


from pandas_datareader.base import _BaseReader

from pandas_datareader.compat import string_types

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

def read(self):
if isinstance(self.symbols, compat.string_types):
if isinstance(self.symbols, string_types):
return self._read_one_data(self.url, self.params(self.symbols))
else:
data = OrderedDict()
Expand Down

0 comments on commit 35189ec

Please sign in to comment.