Skip to content

Commit f34b04c

Browse files
committed
DOC: Note that StataReaders are context managers
1 parent d72d5f9 commit f34b04c

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
@@ -6033,6 +6033,14 @@ values will have ``object`` data type.
60336033
``int64`` for all integer types and ``float64`` for floating point data. By default,
60346034
the Stata data types are preserved when importing.
60356035

6036+
.. note::
6037+
6038+
All :class:`~pandas.io.stata.StataReader` objects, whether created by :func:`~pandas.read_stata`
6039+
(when using ``iterator=True`` or ``chunksize``) or instantiated by hand, must be used as context
6040+
managers (e.g. the ``with`` statement).
6041+
While the :meth:`~pandas.io.stata.StataReader.close` method is available, its use is unsupported.
6042+
It is not part of the public API and will be removed in with future without warning.
6043+
60366044
.. ipython:: python
60376045
:suppress:
60386046

pandas/io/stata.py

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

192192
_read_method_doc = f"""\

0 commit comments

Comments
 (0)