Skip to content

Commit 7aab3b4

Browse files
authored
read_fwf with urlopen test GH#26376 (#52233)
Signed-off-by: Liang Yan <ckgppl_yan@sina.cn>
1 parent ca1bfde commit 7aab3b4

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

pandas/_testing/_io.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ def can_connect(url, error_classes=None) -> bool:
271271
try:
272272
with urlopen(url, timeout=20) as response:
273273
# Timeout just in case rate-limiting is applied
274-
if response.status != 200:
274+
if (
275+
response.info().get("Content-type") == "text/html"
276+
and response.status != 200
277+
):
275278
return False
276279
except error_classes:
277280
return False

pandas/tests/io/parser/test_read_fwf.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from pandas.tests.io.test_compression import _compression_to_extension
3030

31+
from pandas.io.common import urlopen
3132
from pandas.io.parsers import (
3233
read_csv,
3334
read_fwf,
@@ -1010,3 +1011,50 @@ def test_invalid_dtype_backend():
10101011
)
10111012
with pytest.raises(ValueError, match=msg):
10121013
read_fwf("test", dtype_backend="numpy")
1014+
1015+
1016+
@pytest.mark.network
1017+
@tm.network(
1018+
url="ftp://ftp.ncdc.noaa.gov/pub/data/igra/igra2-station-list.txt",
1019+
check_before_test=True,
1020+
)
1021+
def test_url_urlopen():
1022+
expected = pd.Index(
1023+
[
1024+
"CC",
1025+
"Network",
1026+
"Code",
1027+
"StationId",
1028+
"Latitude",
1029+
"Longitude",
1030+
"Elev",
1031+
"dummy",
1032+
"StationName",
1033+
"From",
1034+
"To",
1035+
"Nrec",
1036+
],
1037+
dtype="object",
1038+
)
1039+
url = "ftp://ftp.ncdc.noaa.gov/pub/data/igra/igra2-station-list.txt"
1040+
with urlopen(url) as f:
1041+
result = read_fwf(
1042+
f,
1043+
widths=(2, 1, 3, 5, 9, 10, 7, 4, 30, 5, 5, 7),
1044+
names=(
1045+
"CC",
1046+
"Network",
1047+
"Code",
1048+
"StationId",
1049+
"Latitude",
1050+
"Longitude",
1051+
"Elev",
1052+
"dummy",
1053+
"StationName",
1054+
"From",
1055+
"To",
1056+
"Nrec",
1057+
),
1058+
).columns
1059+
1060+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)