Skip to content

Commit 6846ba1

Browse files
authored
Merge pull request #887 from bashtage/final-fixes
MAINT: Final fixes
2 parents 06446e2 + 7de905d commit 6846ba1

14 files changed

+149
-105
lines changed

pandas_datareader/_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ def _sanitize_dates(start, end):
5353
return start, end
5454

5555

56-
def _init_session(session, retry_count=3):
56+
def _init_session(session):
5757
if session is None:
5858
session = requests.Session()
5959
# do not set requests max_retries here to support arbitrary pause
60+
else:
61+
if not isinstance(session, requests.Session):
62+
raise TypeError("session must be a request.Session")
6063
return session

pandas_datareader/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(
7070
self.pause = pause
7171
self.timeout = timeout
7272
self.pause_multiplier = 1
73-
self.session = _init_session(session, retry_count)
73+
self.session = _init_session(session)
7474
self.freq = freq
7575
self.headers = None
7676

@@ -148,11 +148,7 @@ def _get_response(self, url, params=None, headers=None):
148148
params : dict or None
149149
parameters passed to the URL
150150
"""
151-
152-
# Use default headers if not passes and not using a user session
153-
if headers is None:
154-
headers = self.headers
155-
151+
headers = headers or self.headers
156152
pause = self.pause
157153
last_response_text = ""
158154
for _ in range(self.retry_count + 1):

pandas_datareader/compat/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None, compression=None):
3939
# for "get_filepath_or_buffer" starting in pandas >= 0.20.0
4040
if isinstance(filepath_or_buffer, dict):
4141
return filepath_or_buffer, encoding, compression
42-
43-
tmp = com._get_filepath_or_buffer(
44-
filepath_or_buffer, encoding=encoding, compression=None
45-
)
46-
return tmp.filepath_or_buffer, tmp.encoding, tmp.compression
42+
try:
43+
tmp = com._get_filepath_or_buffer(
44+
filepath_or_buffer, encoding=encoding, compression=None
45+
)
46+
return tmp.filepath_or_buffer, tmp.encoding, tmp.compression
47+
except AttributeError:
48+
tmp = com.get_filepath_or_buffer(
49+
filepath_or_buffer, encoding=encoding, compression=None
50+
)
51+
return tmp
4752

4853

4954
string_types = (str,)

pandas_datareader/naver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(
3535
if not isinstance(symbols, string_types):
3636
raise NotImplementedError("Bulk-fetching is not implemented")
3737

38-
super(NaverDailyReader, self).__init__(
38+
super().__init__(
3939
symbols=symbols,
4040
start=start,
4141
end=end,

pandas_datareader/tests/test_econdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_get_tourism(self):
5858
start=pd.Timestamp("2008-01-01"),
5959
end=pd.Timestamp("2012-01-01"),
6060
)
61-
df = df.astype(np.float)
61+
df = df.astype(float)
6262
jp = np.array([8351000, 6790000, 8611000, 6219000, 8368000], dtype=float)
6363
us = np.array(
6464
[175702304, 160507424, 164079728, 167600272, 171320416], dtype=float

pandas_datareader/tests/test_famafrench.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import pandas as pd
23
from pandas import testing as tm
34
import pytest
@@ -48,49 +49,49 @@ def test_f_f_research(self):
4849
{
4950
"Mkt-RF": [
5051
-3.36,
51-
3.4,
52+
3.40,
5253
6.31,
53-
2.0,
54+
2.00,
5455
-7.89,
55-
-5.56,
56+
-5.57,
5657
6.93,
5758
-4.77,
5859
9.54,
5960
3.88,
60-
0.6,
61+
0.60,
6162
6.82,
6263
],
6364
"SMB": [
64-
0.38,
65-
1.2,
66-
1.42,
67-
4.98,
68-
0.05,
69-
-1.97,
70-
0.16,
71-
-3.00,
72-
3.92,
73-
1.15,
74-
3.70,
75-
0.7,
65+
0.37,
66+
1.19,
67+
1.44,
68+
4.86,
69+
0.14,
70+
-1.84,
71+
0.18,
72+
-3.02,
73+
3.93,
74+
1.07,
75+
3.78,
76+
0.68,
7677
],
7778
"HML": [
78-
0.31,
79-
3.16,
80-
2.1,
81-
2.81,
82-
-2.38,
83-
-4.5,
84-
-0.27,
85-
-1.95,
86-
-3.12,
87-
-2.59,
88-
-0.9,
89-
3.81,
79+
0.33,
80+
3.19,
81+
2.11,
82+
2.91,
83+
-2.39,
84+
-4.52,
85+
-0.36,
86+
-1.90,
87+
-3.23,
88+
-2.46,
89+
-0.95,
90+
3.64,
9091
],
9192
"RF": [
92-
0.0,
93-
0.0,
93+
0.00,
94+
0.00,
9495
0.01,
9596
0.01,
9697
0.01,
@@ -106,7 +107,10 @@ def test_f_f_research(self):
106107
index=pd.period_range("2010-01-01", "2010-12-01", freq="M", name="Date"),
107108
columns=["Mkt-RF", "SMB", "HML", "RF"],
108109
)
109-
tm.assert_frame_equal(results[0], exp, check_less_precise=0)
110+
received = results[0]
111+
np.testing.assert_allclose(received, exp)
112+
tm.assert_index_equal(received.index, exp.index)
113+
tm.assert_index_equal(received.columns, exp.columns)
110114

111115
def test_me_breakpoints(self):
112116
results = web.DataReader(

pandas_datareader/tests/test_fred.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def test_fred_multi(self): # pragma: no cover
7272
)
7373
expected.index.rename("DATE", inplace=True)
7474
expected.index.freq = "MS"
75-
tm.assert_frame_equal(received, expected, check_less_precise=True)
75+
np.testing.assert_allclose(received, expected)
76+
tm.assert_index_equal(received.index, expected.index)
77+
tm.assert_index_equal(received.columns, expected.columns)
7678

7779
def test_fred_multi_bad_series(self):
7880
names = ["NOTAREALSERIES", "CPIAUCSL", "ALSO FAKE"]

pandas_datareader/tests/yahoo/test_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pandas_datareader import data as web
1010

1111

12-
@pytest.yield_fixture
12+
@pytest.fixture
1313
def aapl():
1414
aapl = web.Options("aapl", "yahoo")
1515
yield aapl

pandas_datareader/tests/yahoo/test_yahoo.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_get_data_null_as_missing_data(self, adj_pr):
159159
else:
160160
floats.append("Adj Close")
161161

162-
assert result[floats].dtypes.all() == np.floating
162+
assert result[floats].dtypes.all() == np.float64
163163

164164
@skip_on_exception(RemoteDataError)
165165
def test_get_data_multiple_symbols_two_dates(self):
@@ -168,7 +168,7 @@ def test_get_data_multiple_symbols_two_dates(self):
168168
assert result.size == 3
169169

170170
# sanity checking
171-
assert result.dtypes == np.floating
171+
assert result.dtypes == np.float64
172172

173173
expected = np.array(
174174
[
@@ -207,12 +207,12 @@ def test_get_data_yahoo_actions(self):
207207
assert actions.loc["2005-02-28", "value"][0] == 1 / 2.0
208208

209209
assert actions.loc["1995-11-21", "action"][0] == "DIVIDEND"
210-
assert round(actions.loc["1995-11-21", "value"][0], 3) == 0.120
210+
assert round(actions.loc["1995-11-21", "value"][0], 3) == 0.030
211211

212212
actions = web.get_data_yahoo_actions("AAPL", start, end, adjust_dividends=True)
213213

214214
assert actions.loc["1995-11-21", "action"][0] == "DIVIDEND"
215-
assert round(actions.loc["1995-11-21", "value"][0], 4) == 0.0043
215+
assert round(actions.loc["1995-11-21", "value"][0], 4) == 0.0011
216216

217217
def test_get_data_yahoo_actions_invalid_symbol(self):
218218
start = datetime(1990, 1, 1)
@@ -226,14 +226,14 @@ def test_yahoo_reader_class(self):
226226
r = YahooDailyReader("GOOG", start="JAN-01-2015")
227227
df = r.read()
228228

229-
assert df.Volume.loc["JAN-02-2015"] == 1447500
229+
assert df.Volume.loc["JAN-02-2015"] == 1447563
230230

231231
session = requests.Session()
232232

233233
r = YahooDailyReader("GOOG", session=session)
234234
assert r.session is session
235235

236-
def test_yahoo_DataReader(self):
236+
def test_yahoo_datareader(self):
237237
start = datetime(2010, 1, 1)
238238
end = datetime(2015, 5, 9)
239239
# yahoo will adjust for dividends by default
@@ -275,19 +275,19 @@ def test_yahoo_DataReader(self):
275275
"DIVIDEND",
276276
],
277277
"value": [
278-
0.52,
279-
0.47,
280-
0.47,
281-
0.47,
282-
0.14285714,
283-
0.47,
284-
0.43571,
285-
0.43571,
286-
0.43571,
287-
0.43571,
288-
0.37857,
289-
0.37857,
290-
0.37857,
278+
0.130000,
279+
0.117500,
280+
0.117500,
281+
0.117500,
282+
0.142857,
283+
0.117500,
284+
0.108929,
285+
0.108929,
286+
0.108929,
287+
0.108929,
288+
0.094643,
289+
0.094643,
290+
0.094643,
291291
],
292292
},
293293
index=exp_idx,
@@ -316,19 +316,19 @@ def test_yahoo_DataReader(self):
316316
"DIVIDEND",
317317
],
318318
"value": [
319-
0.52,
320-
0.47,
321-
0.47,
322-
0.47,
323-
0.14285714,
324-
3.29,
325-
3.05,
326-
3.05,
327-
3.05,
328-
3.05,
329-
2.65,
330-
2.65,
331-
2.65,
319+
0.1300,
320+
0.1175,
321+
0.1175,
322+
0.1175,
323+
0.1429,
324+
0.8225,
325+
0.7625,
326+
0.7625,
327+
0.7625,
328+
0.7625,
329+
0.6625,
330+
0.6625,
331+
0.6625,
332332
],
333333
},
334334
index=exp_idx,
@@ -344,13 +344,13 @@ def test_yahoo_DataReader(self):
344344
result = web.DataReader("NTR", "yahoo-actions", start, end)
345345

346346
exp_idx = pd.DatetimeIndex(
347-
["2018-12-28", "2018-09-27", "2018-06-28", "2018-03-28", "2018-01-02"]
347+
["2018-12-28", "2018-09-27", "2018-06-28", "2018-03-28"]
348348
)
349349

350350
exp = pd.DataFrame(
351351
{
352-
"action": ["DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "SPLIT"],
353-
"value": [0.43, 0.40, 0.40, 0.40, 1.00],
352+
"action": ["DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND"],
353+
"value": [0.43, 0.40, 0.40, 0.40],
354354
},
355355
index=exp_idx,
356356
)

pandas_datareader/yahoo/_headers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Default header
3+
"""
4+
DEFAULT_HEADERS = {
5+
"Connection": "keep-alive",
6+
"Expires": str(-1),
7+
"Upgrade-Insecure-Requests": str(1),
8+
# Google Chrome:
9+
"User-Agent": (
10+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
11+
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
12+
),
13+
}

pandas_datareader/yahoo/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class YahooActionReader(YahooDailyReader):
1212
"""
1313

1414
def read(self):
15-
data = super(YahooActionReader, self).read()
15+
data = super().read()
1616
actions = {}
1717
if isinstance(data.columns, MultiIndex):
1818
data = data.swaplevel(0, 1, axis=1)

0 commit comments

Comments
 (0)