From 1e9169505d3809573ef16f4e9fc1df19dd278e93 Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 10 Mar 2022 16:50:46 +0100 Subject: [PATCH 1/4] DOC: Add TextFileReader to docs --- doc/source/reference/io.rst | 13 +++++++++++++ pandas/io/parsers/readers.py | 3 +++ 2 files changed, 16 insertions(+) diff --git a/doc/source/reference/io.rst b/doc/source/reference/io.rst index 70fd381bffd2c..c970c29328d37 100644 --- a/doc/source/reference/io.rst +++ b/doc/source/reference/io.rst @@ -25,6 +25,19 @@ Flat file DataFrame.to_csv read_fwf +.. currentmodule:: pandas.io.parsers + +.. autosummary:: + :toctree: api/ + + TextFileReader + + TextFileReader.get_chunk + TextFileReader.close + TextFileReader.read + +.. currentmodule:: pandas + Clipboard ~~~~~~~~~ .. autosummary:: diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 7684fa32fbd66..931f72515a76e 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -1362,6 +1362,9 @@ class TextFileReader(abc.Iterator): Passed dialect overrides any of the related parser options + Only __enter__, __exit__ and __next__ are public. All other + attributes are considered private and can change. + """ def __init__( From d10d8297ebb50d9e4cd4a6da247efd08d21240d9 Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 10 Mar 2022 17:14:13 +0100 Subject: [PATCH 2/4] Fix docstring --- pandas/io/parsers/readers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 931f72515a76e..81e2599ede608 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -1359,12 +1359,10 @@ def read_fwf( class TextFileReader(abc.Iterator): """ - - Passed dialect overrides any of the related parser options + Passed dialect overrides any of the related parser options. Only __enter__, __exit__ and __next__ are public. All other attributes are considered private and can change. - """ def __init__( From 1e487ad5e3a1c8def55e2d8309017ab1871673a6 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 24 Jun 2022 16:43:51 +0200 Subject: [PATCH 3/4] Add docstrings --- pandas/io/__init__.py | 3 ++- pandas/io/parsers/readers.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pandas/io/__init__.py b/pandas/io/__init__.py index bd3ddc09393d8..df868e0eda561 100644 --- a/pandas/io/__init__.py +++ b/pandas/io/__init__.py @@ -5,8 +5,9 @@ from pandas.io import ( formats, json, + parsers, stata, ) # and mark only those modules as public - __all__ = ["formats", "json", "stata"] + __all__ = ["formats", "json", "parsers", "stata"] diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 38a5f209643c0..937fce0aff8a7 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -1369,8 +1369,19 @@ class TextFileReader(abc.Iterator): """ Passed dialect overrides any of the related parser options. - Only __enter__, __exit__ and __next__ are public. All other - attributes are considered private and can change. + Iterator class used to process to text files read via read_csv in + chunks. + + An instance of this class is returned by `read_csv` when it is processed in + chunks, instead of returning a single `DataFrame`. + + When iterating over `TextFileReader`, every item returned will be a DataFrame. + + Examples + --------- + >>> with pandas.read_csv(..., iterator=True) as text_file_reader: + ... for df in text_file_reader: + ... ... """ def __init__( @@ -1423,6 +1434,7 @@ def __init__( self._engine = self._make_engine(f, self.engine) def close(self) -> None: + """Closes the file handle.""" if self.handles is not None: self.handles.close() self._engine.close() @@ -1731,6 +1743,18 @@ def _failover_to_python(self) -> None: raise AbstractMethodError(self) def read(self, nrows: int | None = None) -> DataFrame: + """ + Reads the text file and stores the result in a DataFrame. + + Parameters + ---------- + nrows: int, optional, default None + The number of rows to read in one go. + + Returns + ------- + DataFrame + """ if self.engine == "pyarrow": try: # error: "ParserBase" has no attribute "read" From 01922673b37a78109aaf5ff0b37caad66048fe86 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Thu, 5 Jan 2023 12:03:51 +0700 Subject: [PATCH 4/4] Update pandas/io/parsers/readers.py --- pandas/io/parsers/readers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 937fce0aff8a7..0697da8bb96cd 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -1379,7 +1379,7 @@ class TextFileReader(abc.Iterator): Examples --------- - >>> with pandas.read_csv(..., iterator=True) as text_file_reader: + >>> with pd.read_csv(..., iterator=True) as text_file_reader: ... for df in text_file_reader: ... ... """