Skip to content

Commit 4792fe8

Browse files
committed
DOC: allow reader files to be closed in doc builds (GH5933)
1 parent 1fab6fc commit 4792fe8

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

doc/source/io.rst

+30-16
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ Consider a typical CSV file containing, in this case, some time series data:
196196

197197
.. ipython:: python
198198
199-
print(open('foo.csv').read())
199+
with open('foo.csv') as fh:
200+
print(fh.read())
200201
201202
The default for `read_csv` is to create a DataFrame with simple numbered rows:
202203

@@ -485,7 +486,8 @@ column names:
485486
486487
.. ipython:: python
487488
488-
print(open('tmp.csv').read())
489+
with open('tmp.csv') as fh:
490+
print(fh.read())
489491
df = pd.read_csv('tmp.csv', header=None, parse_dates=[[1, 2], [1, 3]])
490492
df
491493
@@ -666,7 +668,8 @@ DD/MM/YYYY instead. For convenience, a ``dayfirst`` keyword is provided:
666668
667669
.. ipython:: python
668670
669-
print(open('tmp.csv').read())
671+
with open('tmp.csv') as fh:
672+
print(fh.read())
670673
671674
pd.read_csv('tmp.csv', parse_dates=[0])
672675
pd.read_csv('tmp.csv', dayfirst=True, parse_dates=[0])
@@ -694,7 +697,8 @@ By default, numbers with a thousands separator will be parsed as strings
694697

695698
.. ipython:: python
696699
697-
print(open('tmp.csv').read())
700+
with open('tmp.csv') as fh:
701+
print(fh.read())
698702
df = pd.read_csv('tmp.csv', sep='|')
699703
df
700704
@@ -704,7 +708,8 @@ The ``thousands`` keyword allows integers to be parsed correctly
704708

705709
.. ipython:: python
706710
707-
print(open('tmp.csv').read())
711+
with open(open('tmp.csv') as fh:
712+
print(fh.read())
708713
df = pd.read_csv('tmp.csv', sep='|', thousands=',')
709714
df
710715
@@ -781,7 +786,8 @@ Sometimes comments or meta data may be included in a file:
781786
782787
.. ipython:: python
783788
784-
print(open('tmp.csv').read())
789+
with open('tmp.csv') as fh:
790+
print(fh.read())
785791
786792
By default, the parse includes the comments in the output:
787793
@@ -821,7 +827,8 @@ as a ``Series``:
821827
822828
.. ipython:: python
823829
824-
print(open('tmp.csv').read())
830+
with open('tmp.csv') as fh:
831+
print(fh.read())
825832
826833
output = pd.read_csv('tmp.csv', squeeze=True)
827834
output
@@ -933,7 +940,8 @@ Consider a typical fixed-width data file:
933940
934941
.. ipython:: python
935942
936-
print(open('bar.csv').read())
943+
with open('bar.csv') as fh:
944+
print(fh.read())
937945
938946
In order to parse this file into a DataFrame, we simply need to supply the
939947
column specifications to the `read_fwf` function along with the file name:
@@ -991,7 +999,8 @@ column:
991999
9921000
.. ipython:: python
9931001
994-
print(open('foo.csv').read())
1002+
with open('foo.csv') as fh:
1003+
print(fh.read())
9951004
9961005
In this special case, ``read_csv`` assumes that the first column is to be used
9971006
as the index of the DataFrame:
@@ -1023,7 +1032,8 @@ Suppose you have data indexed by two columns:
10231032
10241033
.. ipython:: python
10251034
1026-
print(open('data/mindex_ex.csv').read())
1035+
with open('data/mindex_ex.csv') as fh:
1036+
print(fh.read())
10271037
10281038
The ``index_col`` argument to ``read_csv`` and ``read_table`` can take a list of
10291039
column numbers to turn multiple columns into a ``MultiIndex`` for the index of the
@@ -1050,7 +1060,8 @@ of tupleizing columns, specify ``tupleize_cols=True``.
10501060
from pandas.util.testing import makeCustomDataframe as mkdf
10511061
df = mkdf(5,3,r_idx_nlevels=2,c_idx_nlevels=4)
10521062
df.to_csv('mi.csv')
1053-
print(open('mi.csv').read())
1063+
with open('mi.csv') as fh:
1064+
print(fh.read())
10541065
pd.read_csv('mi.csv',header=[0,1,2,3],index_col=[0,1])
10551066
10561067
Starting in 0.13.0, ``read_csv`` will be able to interpret a more common format
@@ -1066,7 +1077,8 @@ of multi-columns indices.
10661077
10671078
.. ipython:: python
10681079
1069-
print(open('mi2.csv').read())
1080+
with open('mi2.csv') as fh:
1081+
print(fh.read())
10701082
pd.read_csv('mi2.csv',header=[0,1],index_col=0)
10711083
10721084
Note: If an ``index_col`` is not specified (e.g. you don't have an index, or wrote it
@@ -1097,8 +1109,9 @@ class of the csv module. For this, you have to specify ``sep=None``.
10971109
10981110
.. ipython:: python
10991111
1100-
print(open('tmp2.sv').read())
1101-
pd.read_csv('tmp2.sv', sep=None)
1112+
with open('tmp2.sv') as fh:
1113+
print(fh.read())
1114+
pd.read_csv('tmp2.sv', sep=None, engine='python')
11021115
11031116
.. _io.chunking:
11041117
@@ -1111,7 +1124,8 @@ rather than reading the entire file into memory, such as the following:
11111124
11121125
.. ipython:: python
11131126
1114-
print(open('tmp.sv').read())
1127+
with open('tmp.sv') as fh:
1128+
print(fh.read())
11151129
table = pd.read_table('tmp.sv', sep='|')
11161130
table
11171131
@@ -1127,7 +1141,6 @@ value will be an iterable object of type ``TextFileReader``:
11271141
for chunk in reader:
11281142
print(chunk)
11291143
1130-
11311144
Specifying ``iterator=True`` will also return the ``TextFileReader`` object:
11321145
11331146
.. ipython:: python
@@ -1138,6 +1151,7 @@ Specifying ``iterator=True`` will also return the ``TextFileReader`` object:
11381151
.. ipython:: python
11391152
:suppress:
11401153
1154+
reader = None
11411155
os.remove('tmp.sv')
11421156
os.remove('tmp2.sv')
11431157

0 commit comments

Comments
 (0)