Skip to content

Commit 7ec5ece

Browse files
committed
DOC: Note that StataReaders are context managers
1 parent 8775e24 commit 7ec5ece

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

doc/source/user_guide/io.rst

+8
Original file line numberDiff line numberDiff line change
@@ -6028,6 +6028,14 @@ values will have ``object`` data type.
60286028
``int64`` for all integer types and ``float64`` for floating point data. By default,
60296029
the Stata data types are preserved when importing.
60306030

6031+
.. note::
6032+
6033+
All :class:`~pandas.io.stata.StataReader` objects, whether created by :func:`~pandas.read_stata`
6034+
(when using ``iterator=True`` or ``chunksize``) or instantiated by hand, must be used as context
6035+
managers (e.g. the ``with`` statement).
6036+
The :meth:`~pandas.io.stata.StataReader.close` method is available, but is unsupported, not part
6037+
of the public API, and may be removed in the future.
6038+
60316039
.. ipython:: python
60326040
:suppress:
60336041

pandas/io/stata.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@
182182
>>> df = pd.DataFrame(values, columns=["i"]) # doctest: +SKIP
183183
>>> df.to_stata('filename.dta') # doctest: +SKIP
184184
185-
>>> itr = pd.read_stata('filename.dta', chunksize=10000) # doctest: +SKIP
186-
>>> for chunk in itr:
187-
... # Operate on a single chunk, e.g., chunk.mean()
188-
... pass # doctest: +SKIP
185+
>>> with pd.read_stata('filename.dta', chunksize=10000) as itr: # doctest: +SKIP
186+
>>> for chunk in itr:
187+
... # Operate on a single chunk, e.g., chunk.mean()
188+
... pass # doctest: +SKIP
189189
"""
190190

191191
_read_method_doc = f"""\

0 commit comments

Comments
 (0)