Skip to content

Commit d94fa66

Browse files
Chang Shewesm
Chang She
authored andcommitted
DOC: started on docs for parse_dates and date_parser
1 parent ea158f7 commit d94fa66

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

doc/source/io.rst

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ And then import the data directly to a DataFrame by calling:
4949
5050
clipdf
5151
52+
.. _io.read_csv_table:
53+
5254
CSV & Text files
5355
----------------
5456

55-
.. _io.parse_dates:
56-
5757
The two workhorse functions for reading text files (a.k.a. flat files) are
5858
:func:`~pandas.io.parsers.read_csv` and :func:`~pandas.io.parsers.read_table`.
5959
They both use the same parsing code to intelligently convert tabular
@@ -118,9 +118,16 @@ The default for `read_csv` is to create a DataFrame with simple numbered rows:
118118
read_csv('foo.csv')
119119
120120
In the case of indexed data, you can pass the column number (or a list of
121-
column numbers, for a hierarchical index) you wish to use as the index. If the
122-
index values are dates and you want them to be converted to ``datetime``
123-
objects, pass ``parse_dates=True``:
121+
column numbers, for a hierarchical index) you wish to use as the index.
122+
123+
.. _io.parse_dates:
124+
125+
To better facilitate working with datetime data, :func:`~pandas.io.parsers.read_csv` and :func:`~pandas.io.parsers.read_table`
126+
uses the keyword arguments ``parse_dates`` and ``date_parser`` to allow users
127+
to specify a variety of columns and date/time formats to turn the input text
128+
data into ``datetime`` objects.
129+
130+
The simplest case is to just pass in ``parse_dates=True``:
124131

125132
.. ipython:: python
126133
@@ -135,6 +142,31 @@ objects, pass ``parse_dates=True``:
135142
136143
os.remove('foo.csv')
137144
145+
You can specify a custom ``date_parser`` function:
146+
147+
.. ipython:: python
148+
:suppress:
149+
# data = """
150+
with open('tmp.csv', 'w') as fh:
151+
fh.write(data)
152+
153+
.. ipython:: python
154+
155+
# read it in
156+
157+
.. ipython:: python
158+
:suppress:
159+
os.remove('tmp.csv')
160+
161+
It is often the case that we may want to store date and time data separately,
162+
or store various date fields separately. the ``parse_dates`` keyword can be
163+
used to specify a combination of columns to parse the dates and/or times from.
164+
165+
You can specify a list of column lists to ``parse_dates``, the resulting date
166+
columns will be prepended to the output and the new column names will be the
167+
component column names
168+
169+
138170
The parsers make every attempt to "do the right thing" and not be very
139171
fragile. Type inference is a pretty big deal. So if a column can be coerced to
140172
integer dtype without altering the contents, it will do so. Any non-numeric

0 commit comments

Comments
 (0)