Skip to content

Commit 246f507

Browse files
gfyoungjreback
authored andcommitted
MAINT: Refactor more compat checks into compat init (#311)
1 parent d5dfe74 commit 246f507

File tree

6 files changed

+23
-54
lines changed

6 files changed

+23
-54
lines changed

pandas_datareader/_utils.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
import datetime as dt
2-
from distutils.version import LooseVersion
3-
4-
import pandas as pd
52
from pandas import to_datetime
3+
from requests_file import FileAdapter
4+
from pandas_datareader.compat import is_number
65

76
import requests
8-
from requests_file import FileAdapter
97
import requests_ftp
10-
requests_ftp.monkeypatch_session()
11-
12-
13-
if pd.compat.PY3:
14-
from urllib.error import HTTPError # noqa
15-
else:
16-
from urllib2 import HTTPError # noqa
178

18-
PANDAS_VERSION = LooseVersion(pd.__version__)
19-
20-
if PANDAS_VERSION >= LooseVersion('0.19.0'):
21-
PANDAS_0190 = True
22-
from pandas.api.types import is_number # noqa
23-
else:
24-
PANDAS_0190 = False
25-
from pandas.core.common import is_number # noqa
26-
27-
if PANDAS_VERSION >= LooseVersion('0.17.0'):
28-
PANDAS_0170 = True
29-
else:
30-
PANDAS_0170 = False
31-
32-
if PANDAS_VERSION >= LooseVersion('0.16.0'):
33-
PANDAS_0160 = True
34-
else:
35-
PANDAS_0160 = False
36-
37-
if PANDAS_VERSION >= LooseVersion('0.14.0'):
38-
PANDAS_0140 = True
39-
else:
40-
PANDAS_0140 = False
9+
requests_ftp.monkeypatch_session()
4110

4211

4312
class SymbolWarning(UserWarning):

pandas_datareader/compat/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# flake8: noqa
22
import pandas as pd
3+
import pandas.compat as compat
34

45
from io import BytesIO
56
from distutils.version import LooseVersion
67

7-
pandas_version = LooseVersion(pd.__version__)
8-
if pandas_version <= '0.19.2':
8+
PANDAS_VERSION = LooseVersion(pd.__version__)
9+
10+
PANDAS_0190 = (PANDAS_VERSION >= LooseVersion('0.19.0'))
11+
PANDAS_0170 = (PANDAS_VERSION >= LooseVersion('0.17.0'))
12+
PANDAS_0160 = (PANDAS_VERSION >= LooseVersion('0.16.0'))
13+
14+
if PANDAS_0190:
15+
from pandas.api.types import is_number
16+
else:
917
from pandas.core.common import is_number
18+
19+
if compat.PY3:
20+
from urllib.error import HTTPError
1021
else:
11-
from pandas.api.types import is_number
22+
from urllib2 import HTTPError

pandas_datareader/io/sdmx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas.compat as compat
99

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

1313

1414
_STRUCTURE = '{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure}'

pandas_datareader/tests/test_eurostat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas.util.testing as tm
77
import pandas_datareader.data as web
88

9-
from pandas_datareader._utils import PANDAS_0170
9+
from pandas_datareader.compat import PANDAS_0170
1010

1111

1212
class TestEurostat(tm.TestCase):

pandas_datareader/tests/test_wb.py

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

99
from pandas_datareader.wb import (search, download, get_countries,
1010
get_indicators, WorldBankReader)
11-
from pandas_datareader._utils import PANDAS_0170, PANDAS_0160, PANDAS_0140
11+
from pandas_datareader.compat import PANDAS_0170, PANDAS_0160
1212

1313

1414
class TestWB(tm.TestCase):
@@ -70,12 +70,7 @@ def test_wdi_download(self):
7070
# Round, to ignore revisions to data.
7171
result = np.round(result, decimals=-3)
7272

73-
if PANDAS_0140:
74-
expected.index.names = ['country', 'year']
75-
else:
76-
# prior versions doesn't allow to set multiple names to MultiIndex
77-
# Thus overwrite it with the result
78-
expected.index = result.index
73+
expected.index.names = ['country', 'year']
7974
tm.assert_frame_equal(result, expected)
8075

8176
# pass start and end as string
@@ -114,13 +109,7 @@ def test_wdi_download_str(self):
114109
result = result.sort()
115110
result = np.round(result, decimals=-3)
116111

117-
if PANDAS_0140:
118-
expected.index.names = ['country', 'year']
119-
else:
120-
# prior versions doesn't allow to set multiple names to MultiIndex
121-
# Thus overwrite it with the result
122-
expected.index = result.index
123-
112+
expected.index.names = ['country', 'year']
124113
tm.assert_frame_equal(result, expected)
125114

126115
result = WorldBankReader(inds, countries=cntry_codes,

pandas_datareader/wb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from pandas_datareader.base import _BaseReader
10-
from pandas_datareader._utils import PANDAS_0170
10+
from pandas_datareader.compat import PANDAS_0170
1111

1212
# This list of country codes was pulled from wikipedia during October 2014.
1313
# While some exceptions do exist, it is the best proxy for countries supported

0 commit comments

Comments
 (0)