Repository: pandas-dev/pandas

Issue:
read_fwf() function does not infer compression by default
#### Code Sample, a copy-pastable example if possible
```python
import io
import gzip
import os
import pandas as pd

fwf = b"""a b c\n1 2 3\n4 5 6"""
with gzip.open('file.gz', 'wb') as gz:
    gz.write(fwf)
print(pd.read_fwf('file.gz', compression='infer'))
print(pd.read_fwf('file.gz'))
os.remove('file.gz')
```
#### Problem description
The pandas documentation indicates that read_fwf() (as with read_csv()) infers a file's compression type based on its extension _by default_, but it does not.

#### Expected Output
I expect 2 identical copies of the dataframe to be displayed.

#### Output of ``pd.show_versions()``

<details>


INSTALLED VERSIONS
------------------
commit: 776fed3ab63d74ddef6e5af1a702b10c2a30bbb6
python: 3.7.0.final.0
python-bits: 64
OS: Darwin
OS-release: 17.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.0.dev0+418.g776fed3ab
pytest: 3.7.1
pip: 10.0.1
setuptools: 39.0.1
Cython: 0.28.5
numpy: 1.15.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: 1.7.6
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None

</details>



Discussion:

Resolving patch:
diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py
--- a/pandas/io/parsers.py
+++ b/pandas/io/parsers.py
@@ -401,7 +401,7 @@ def _read(filepath_or_buffer, kwds):
         encoding = re.sub('_', '-', encoding).lower()
         kwds['encoding'] = encoding
 
-    compression = kwds.get('compression')
+    compression = kwds.get('compression', 'infer')
     compression = _infer_compression(filepath_or_buffer, compression)
     filepath_or_buffer, _, compression, should_close = get_filepath_or_buffer(
         filepath_or_buffer, encoding, compression)


Test patch:
diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py
--- a/pandas/tests/io/parser/test_read_fwf.py
+++ b/pandas/tests/io/parser/test_read_fwf.py
@@ -555,20 +555,26 @@ def test_default_delimiter():
     tm.assert_frame_equal(result, expected)
 
 
-@pytest.mark.parametrize("compression", ["gzip", "bz2"])
-def test_fwf_compression(compression):
+@pytest.mark.parametrize("infer", [True, False, None])
+def test_fwf_compression(compression_only, infer):
     data = """1111111111
     2222222222
     3333333333""".strip()
 
+    compression = compression_only
+    extension = "gz" if compression == "gzip" else compression
+
     kwargs = dict(widths=[5, 5], names=["one", "two"])
     expected = read_fwf(StringIO(data), **kwargs)
 
     if compat.PY3:
         data = bytes(data, encoding="utf-8")
 
-    with tm.ensure_clean() as path:
+    with tm.ensure_clean(filename="tmp." + extension) as path:
         tm.write_to_compressed(compression, path, data)
 
-        result = read_fwf(path, compression=compression, **kwargs)
+        if infer is not None:
+            kwargs["compression"] = "infer" if infer else compression
+
+        result = read_fwf(path, **kwargs)
         tm.assert_frame_equal(result, expected)