Skip to content

read_fwf with urlopen test GH#26376 #52233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ def can_connect(url, error_classes=None) -> bool:
try:
with urlopen(url, timeout=20) as response:
# Timeout just in case rate-limiting is applied
if response.status != 200:
if (
response.info().get("Content-type") == "text/html"
and response.status != 200
):
return False
except error_classes:
return False
Expand Down
48 changes: 48 additions & 0 deletions pandas/tests/io/parser/test_read_fwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from pandas.tests.io.test_compression import _compression_to_extension

from pandas.io.common import urlopen
from pandas.io.parsers import (
read_csv,
read_fwf,
Expand Down Expand Up @@ -1010,3 +1011,50 @@ def test_invalid_dtype_backend():
)
with pytest.raises(ValueError, match=msg):
read_fwf("test", dtype_backend="numpy")


@pytest.mark.network
@tm.network(
url="ftp://ftp.ncdc.noaa.gov/pub/data/igra/igra2-station-list.txt",
check_before_test=True,
)
def test_url_urlopen():
expected = pd.Index(
[
"CC",
"Network",
"Code",
"StationId",
"Latitude",
"Longitude",
"Elev",
"dummy",
"StationName",
"From",
"To",
"Nrec",
],
dtype="object",
)
url = "ftp://ftp.ncdc.noaa.gov/pub/data/igra/igra2-station-list.txt"
with urlopen(url) as f:
result = read_fwf(
f,
widths=(2, 1, 3, 5, 9, 10, 7, 4, 30, 5, 5, 7),
names=(
"CC",
"Network",
"Code",
"StationId",
"Latitude",
"Longitude",
"Elev",
"dummy",
"StationName",
"From",
"To",
"Nrec",
),
).columns

tm.assert_index_equal(result, expected)