Skip to content

Commit 1efa51c

Browse files
chris-b1jorisvandenbossche
authored andcommitted
DOC/TST: dtype param in read_fwf (pandas-dev#14768)
1 parent e299560 commit 1efa51c

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

doc/source/io.rst

+11
Original file line numberDiff line numberDiff line change
@@ -1268,11 +1268,22 @@ is whitespace).
12681268
df = pd.read_fwf('bar.csv', header=None, index_col=0)
12691269
df
12701270
1271+
.. versionadded:: 0.20.0
1272+
1273+
``read_fwf`` supports the ``dtype`` parameter for specifying the types of
1274+
parsed columns to be different from the inferred type.
1275+
1276+
.. ipython:: python
1277+
1278+
pd.read_fwf('bar.csv', header=None, index_col=0).dtypes
1279+
pd.read_fwf('bar.csv', header=None, dtype={2: 'object'}).dtypes
1280+
12711281
.. ipython:: python
12721282
:suppress:
12731283
12741284
os.remove('bar.csv')
12751285
1286+
12761287
Indexes
12771288
'''''''
12781289

doc/source/whatsnew/v0.20.0.txt

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ The ``dtype`` keyword argument in the :func:`read_csv` function for specifying t
3434
pd.read_csv(StringIO(data), engine='python').dtypes
3535
pd.read_csv(StringIO(data), engine='python', dtype={'a':'float64', 'b':'object'}).dtypes
3636

37+
The ``dtype`` keyword argument is also now supported in the :func:`read_fwf` function for parsing
38+
fixed-width text files.
39+
40+
.. ipython:: python
41+
42+
data = "a b\n1 2\n3 4"
43+
pd.read_fwf(StringIO(data)).dtypes
44+
pd.read_fwf(StringIO(data), dtype={'a':'float64', 'b':'object'}).dtypes
45+
3746
.. _whatsnew_0200.enhancements.other:
3847

3948
Other enhancements

pandas/io/tests/parser/test_read_fwf.py

+20
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,23 @@ def test_variable_width_unicode(self):
345345
header=None, encoding='utf8')
346346
tm.assert_frame_equal(expected, read_fwf(
347347
BytesIO(test.encode('utf8')), header=None, encoding='utf8'))
348+
349+
def test_dtype(self):
350+
data = ''' a b c
351+
1 2 3.2
352+
3 4 5.2
353+
'''
354+
colspecs = [(0, 5), (5, 10), (10, None)]
355+
result = pd.read_fwf(StringIO(data), colspecs=colspecs)
356+
expected = pd.DataFrame({
357+
'a': [1, 3],
358+
'b': [2, 4],
359+
'c': [3.2, 5.2]}, columns=['a', 'b', 'c'])
360+
tm.assert_frame_equal(result, expected)
361+
362+
expected['a'] = expected['a'].astype('float64')
363+
expected['b'] = expected['b'].astype(str)
364+
expected['c'] = expected['c'].astype('int32')
365+
result = pd.read_fwf(StringIO(data), colspecs=colspecs,
366+
dtype={'a': 'float64', 'b': str, 'c': 'int32'})
367+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)